Objective-C:我的具有静态方法的类“未实现 methodSignatureForSelector:——前方有麻烦”
我有一个实用程序类,它只有静态方法,所以它不是从 NSObject 继承(不需要,对吗?)
编译时根本没有警告。
在iPhone模拟器上运行时就会出现问题。它崩溃并警告“未实现 methodSignatureForSelector:——前方有麻烦” 嗯,我喜欢“前方有麻烦”的事情,我从来没有听到调试器告诉我有“前方有麻烦”。 但我不喜欢的是错误本身......当我明确调用静态方法时,为什么它期望我在类中实现 methodSignatureForSelector ? (+)
谢谢! 丹尼尔
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这不是 Cocoa 中的惯用模式。我强烈建议您重新考虑您的设计。这不是 Java 或 C++。一方面,甚至不存在“静态方法”这样的东西——它们是类方法,类本身就是一个对象。
拥有一个不是 NSObject 子类的类也很奇怪(“不需要”并不是偏离默认行为的非常合理的理由),更奇怪的是拥有一个仅包含类方法的类。该类可能应该是单例,或者应该被消除,并且其方法变成函数,具体取决于它是否需要保持状态。
至于你崩溃的确切原因,在没有看到你的代码的情况下很难说。该警告本身不应使您的程序崩溃。您确实需要实现 +initialize,即使它什么也不做,因为运行时会将该消息发送到每个接收消息的类。这可能就是出现错误的地方 - 您发送一条消息,运行时尝试发送
initialize
,您的类没有响应,运行时尝试调用转发机制但不能。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 sendinitialize
, your class doesn't respond, the runtime tries to invoke the forwarding machinery and can't.感谢您的回答!
关于“静态”与“类方法”,据我所知,这只是命名,没有真正的区别。就像“函数/方法”和“消息”一样。
然而,这并不一定是“不正确”的设计。
首先你必须记住 ObjC 没有命名空间,所以对事物进行排序的唯一方法是类。因为如果两个函数的名称冲突,编译器会大声喊叫。
有时有些函数是“实用”函数,作用于其他对象,或进行某些计算,不能直接与某个对象相关来管理它们,而且它们也不应该,因为这只会产生不必要的开销。
作为一名经验丰富的 C/C++/Asm/Others 程序员,在使用 ObjC 编程时,出于性能原因,我倾向于自己释放内存。
出于同样的原因,我不想在不需要的地方产生任何开销。而且 ObjC 的开销很大。
文档也没有说我必须从 NSObject 继承,它说当我希望它由框架正确管理时我应该继承。
但据我了解,不需要任何管理,这些函数应该只是包装在类名的命名空间内的函数。
关于 +initialize - 仅当类继承自 NSObject 时才能覆盖。所以最初的问题仍然存在——如果我不需要 NSObject 的任何服务,为什么我应该继承 NSObject 呢?我不需要分配该类或初始化它,因为我与它的实例无关!
ObjC 中还有一个奇怪的事情是你可以重写类方法?!
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?!