【问题标题】:Objective-C: Class Level Function' meaning? [duplicate]Objective-C:类级别函数的含义? [复制]
【发布时间】:2011-06-29 09:43:21
【问题描述】:

可能重复:
What do the plus and minus signs mean in Objective C next to a method?

我在课堂上对 -(type) 和 +(type) 感到困惑。

@interface Class:Something{

    +(id) foo;
    -(void) fooAgain;
}

+(type) 是类级函数(根据谷歌) 但我不明白类级函数的含义。 +(id)foo 里面有点 {return some value} 我认为它类似于 getter 或 setter 但不知何故它必须不同。 (因为@property 指的是getter/setter)

谁能简单解释一下?谢谢

【问题讨论】:

    标签: iphone objective-c ios class syntax


    【解决方案1】:

    类方法应该使用类名本身来调用。而且,实例方法应该使用类的实例来调用。

    因此,您必须从其他一些类中调用上述方法,如下所示,

    类方法:

    [Something foo]; // Correct
    [Something fooAgain]; // Crash
    

    实例方法:

    Something *aThing = [[Something alloc] init];
    [aThing fooAgain]; // Correct
    [aThing foo]; // Crash
    

    【讨论】:

    • @So_CLOSE_BUT_STILL_SO_FAR,谢谢你的拥抱 ;-)
    猜你喜欢
    • 2012-10-28
    • 2010-11-06
    • 2013-03-03
    • 1970-01-01
    • 2016-04-13
    • 1970-01-01
    • 2011-11-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多