views:

108

answers:

2

Hi!

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

+5  A: 

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.

Chuck
A: 

Hi Chuck!

Thanks for the answer!

About the 'static' vs. 'class methods', AFAIK this is just naming, no real difference. Like 'functions/methods' and 'messages'.

However, this is not necessarily 'incorrect' design. First you have to remember that ObjC has no namespacing, so the only way to put some order into things, is a class. Because if two functions' names collide, the compiler will shout loudly. There ARE sometimes some functions that are 'Utility' functions and work on other objects, or do certain calculations, that can't be directly related to a certain object to manage them, and also they shouldn't, because that will just generate unnecessary overhead.

As a very experienced C/C++/Asm/Others prorgammer, when programming in ObjC, I tend to always release memory myself, for performance reasons. For the same reasons, I wouldn't want to generate any overhead where its not needed. And ObjC has a lot of overhead.

The docs also do not say that I MUST inherit from NSObject, it says that I SHOULD when I want it to be correctly managed by the framework.

But as I understand it there's no need for any managing, these functions should be just functions wrapped inside a classname's namespace.

About +initiallize - that can only be overridden if the class inherits from NSObject. So the original question is still there - why should I inherit from NSObject if I do not want any of its services? I do not need to allocate the class or init it, as I have nothing to do with an instance of it!

Also a weird thing in ObjC is that you can override a class method?!

Daniel
Yes, there are real differences between “static” and “class” methods. A “static” method is syntactic sugar for a function, but an Objective-C class method is a real, dynamic-dispatch method sent to the class object. This is why, as you yourself note, you can override it. This also means that there is overhead to calling a class method, so by your own logic you shouldn’t be using them unnecessarily.
Ahruman
Methods and messages are also different concepts. A message expression in Objective-C does not call a method directly; it dispatches it through a mechanism that can be customized (e.g., by forwarding or by dynamically adding methods). There is usually a one-to-one relationship between methods and messages, but not always.
Ahruman
Well after re-reading what I myself wrote here, I understood that I just answered myself, as you noted...And I think I just figured out where this mess comes from:The nature of ObjC... They combined C with some freak language, and the result is this...What happens is that ObjC doesn't really have 'class'es.It emulates them by encapsulating some management data inside a simple data structure.I don't know why I didn't see this earlier, with all this *pointer always attached to the class, and those "isa" and other stuff.This just explains it all
Daniel
I guess some people like ObjC, but personally I think it was a mistake to invent it.However, this IS the first environment ever that the compiler told me 'Trouble ahead!" :-)
Daniel
Oh, and thanks Ahruman!
Daniel
Objective-C does have classes. It may not have the static method-call binding that you have in other object-oriented languages, but it does have classes, with methods and instances that also have methods. `isa` and other such things are just implementation details; you're not supposed to use them yourself. I think you should not be so quick to dismiss languages that are different from the ones you are used to. Just because it's dynamic dispatch and not static binding doesn't mean it's not OOP.
Peter Hosey
It would be at least as accurate to say that Objective-C (and in particular its dynamic-everything approach) is object-oriented, but C++ and Java are not. If you don’t believe me, ask Alan Kay, who invented the term. :-)
Ahruman
Daniel: Stack Overflow isn't a message board. Rather than create a new ansewr to have a conversation, it's better to either amend your question or reply to an answer with a comment.
Chris Hanson
Well Peter and Ahruman, I'm not arguing whether ObjC is OOP or not, I'm just saying I really don't like the way it implements it, and pointing out that the way it is implemented causes certain limitations that can really pi** off an old-skool programmer...And Chris: I know, I apologize, but I did not use a registered username, and after the first post, the browser did not save my cookie and did not allow me to comment on the original post...
Daniel