Objective-C: my class with static methods “does not implement methodSignatureForSelector: — trouble ahead”

后端 未结 2 2131
抹茶落季
抹茶落季 2021-02-09 23:27

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 pro

相关标签:
2条回答
  • 2021-02-09 23:57

    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?!

    0 讨论(0)
  • 2021-02-10 00:21

    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.

    0 讨论(0)
提交回复
热议问题