代码之家  ›  专栏  ›  技术社区  ›  Richard J. Ross III

静态objective-c类是否必须子类NSObject?

  •  1
  • Richard J. Ross III  · 技术社区  · 14 年前

    在我的objective-c项目中,我有一个奇怪的,比方说, 特征

    #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
    

    1 回复  |  直到 14 年前
        1
  •  2
  •   Wevah    14 年前

    简单的回答是“是”。

    NSObject 协议,最简单的方法是确保对象继承自 NSObject对象