Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C: my class with static methods "does not implement methodSignatureForSelector: -- trouble ahead"

I have a utility class which has only static methods, so it is not inheriting from NSObject (No need right?)

There are no warnings at all when compiling.

The problem comes when running on the iPhone simulator. It crashes with warning "does not implement methodSignatureForSelector: -- trouble ahead" Well, I like that "trouble ahead" thing, I never heard a debugger telling me that there's "trouble ahead". But what I don't like is the error itself... Why is it expecting me to implement methodSignatureForSelector in a class when I clearly call a static method? (+)

Thanks! Daniel

like image 327
Daniel Avatar asked Aug 18 '10 20:08

Daniel


1 Answers

This is not an idiomatic pattern in Cocoa. I would strongly recommend you rethink your design. This is not Java or C++. For one thing, there isn't even such a thing as a "static method" — they're class methods, with the class being an object itself.

It's also very weird to have a class that's not a subclass of NSObject ("no need" is not a very rational reason for deviating from the default behavior), and even weirder to have a class with only class methods. The class should probably either be a singleton or else eliminated and its methods turned into functions, depending on whether it needs to keep state.

As for the exact reason you're crashing, it's hard to say without seeing your code. That warning by itself should not crash your program. You do need to have some implementation of +initialize, even if it does nothing, because the runtime sends that message to every class that receives a message. That's probably where the error is coming up — you send a message, the runtime tries to send initialize, your class doesn't respond, the runtime tries to invoke the forwarding machinery and can't.

like image 86
Chuck Avatar answered Oct 23 '22 04:10

Chuck