iphone - Objective-C:类级别函数的含义?

标签 iphone objective-c ios class syntax

<分区>

Possible Duplicate:
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 内部是一种{返回一些值} 我认为它类似于 getter 或 setter 但不知何故它一定是不同的。 (因为@property 指的是 getter/setter)

谁能简单解释一下?谢谢

最佳答案

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

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

类方法:

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

实例方法:

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

关于iphone - Objective-C:类级别函数的含义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6518633/

上一篇:iphone - iOS 默认文档文件

下一篇:iphone - NSlog 为来自不同类的 NSMutableDictionary 返回 NULL

相关文章:

ios - 通过 Twilio 在 iOS 中使用后台进程

iphone - 对具有类对象的数组进行排序

objective-c - 如何使 UIWebView 在 Objective-C 中不可见

ios - Swift 正确声明对应于协议(protocol)并具有父类(super class)的对象

iOS Autolayout - 框架大小未在 viewDidLayoutSubviews 中设置

iphone - 删除 NSString 左边的空格

ios - 如何在 iOS 的多个 friend 墙上分享 google Plus 中的消息?

IOS-TableView Cell : dequeueReusableCellWithIdentifier issue

iphone - iOS - 后台加速度计显示位置服务弹出窗口,但我们不需要位置服务

ios - 如何在 Swift 的 uiviewcontroller 背景中显示自动滚动的图像?