Objective-C:我的带有静态方法的类“未实现methodSignatureForSelector:-麻烦不断" [英] Objective-C: my class with static methods "does not implement methodSignatureForSelector: -- trouble ahead"

查看:90
本文介绍了Objective-C:我的带有静态方法的类“未实现methodSignatureForSelector:-麻烦不断"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个仅包含静态方法的实用程序类,因此它不是从NSObject继承的(不需要吗?)

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

编译时完全没有警告.

在iPhone模拟器上运行时出现问题.崩溃并显示警告未实现methodSignatureForSelector:-前面有麻烦" 好吧,我喜欢这种麻烦在前"的东西,我从未听说过调试器告诉我麻烦在前". 但是我不喜欢错误本身...为什么当我清楚地调用静态方法时,为什么期望我在一个类中实现methodSignatureForSelector? (+)

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? (+)

谢谢! 丹尼尔

推荐答案

这不是可可中的惯用模式.我强烈建议您重新考虑您的设计.这不是Java或C ++.一方面,甚至没有静态方法"之类的东西-它们是类方法,而类本身就是对象.

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.

有一个不是NSObject子类的类也很奇怪(不需要"不是偏离默认行为的非常合理的理由),甚至有一个只有类方法的类也很奇怪.该类可能应该是单例,或者应该被淘汰,并且根据其是否需要保持状态,将其方法转换为函数.

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.

由于崩溃的确切原因,如果不查看代码就很难说.该警告本身不应使您的程序崩溃.即使不执行任何操作,您也确实需要实现+initialize,因为运行时会将该消息发送给接收消息的每个类.可能就是错误的出现了-您发送一条消息,运行时尝试发送initialize,您的类没有响应,运行时尝试调用转发机制而不能.

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.

这篇关于Objective-C:我的带有静态方法的类“未实现methodSignatureForSelector:-麻烦不断"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆