静态 Objective-C 类必须是 NSObject 的子类吗?
在我的 Objective-C 项目中,我有一个奇怪的,可以说,feature 我有一个类,如下所示:
#import <Foundation/Foundation.h>
@interface Convert /* : NSObject */ // <--- is that necessary?
+(int) toInt:(id) obj;
@end
@implementation Convert
+(int) toInt:(id) obj
{
return [obj intValue];
}
@end
发生的情况是,当我单步执行代码时它工作正常,但我得到一个神秘的控制台中的错误(即使代码完全正常,按预期工作):
2010-11-03 09:35:49.422 Tests[14066:5f03] *** NSInvocation: warning: object 0x9e424 of class 'Convert' does not implement methodSignatureForSelector: -- trouble ahead
2010-11-03 09:35:49.422 Tests[14066:5f03] *** NSInvocation: warning: object 0x9e424 of class 'Convert' does not implement doesNotRecognizeSelector: -- abort
然而,即使它说中止,代码仍然有效。但是,当我运行它而不单步执行这些代码行时,它会中止。发生了什么以及为什么?
In my objective-c project, I have a weird, lets say, feature I have a class, like this:
#import <Foundation/Foundation.h>
@interface Convert /* : NSObject */ // <--- is that necessary?
+(int) toInt:(id) obj;
@end
@implementation Convert
+(int) toInt:(id) obj
{
return [obj intValue];
}
@end
What happens is, when I step through the code It works fine, but I get a cryptic error in the console (even though code is completely fine, works as expected):
2010-11-03 09:35:49.422 Tests[14066:5f03] *** NSInvocation: warning: object 0x9e424 of class 'Convert' does not implement methodSignatureForSelector: -- trouble ahead
2010-11-03 09:35:49.422 Tests[14066:5f03] *** NSInvocation: warning: object 0x9e424 of class 'Convert' does not implement doesNotRecognizeSelector: -- abort
Yet, even when It says abort, the code still works. However, when I run it without stepping through those lines of code, it aborts. What Is happening and why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简单的答案是“是”。
或者更具体地说,运行时期望对象符合
NSObject
协议,而实现这一点的最简单方法是确保您的对象继承自NSObject
类。The simple answer is "yes".
Or more specifically, the runtime expects objects to conform to the
NSObject
protocol, and the simplest way to do this is by making sure your objects inherit from theNSObject
class.