为了账号安全,请及时绑定邮箱和手机立即绑定

方法旁边的目标C中的正号和负号是什么意思?

方法旁边的目标C中的正号和负号是什么意思?

ibeautiful 2019-10-17 10:27:52
我在目标c和xcode中都是新手。我想知道方法定义旁边的+和-符号的含义。- (void)loadPluginsAtPath:(NSString*)pluginPath errors:(NSArray **)errors;
查看完整描述

3 回答

?
慕码人8056858

TA贡献1803条经验 获得超6个赞

+用于类方法和-实例方法。


例如


// Not actually Apple's code.

@interface NSArray : NSObject {

}

+ (NSArray *)array;

- (id)objectAtIndex:(NSUInteger)index;

@end


// somewhere else:


id myArray = [NSArray array];         // see how the message is sent to NSArray?

id obj = [myArray objectAtIndex:4];   // here the message is sent to myArray


// Btw, in production code one uses "NSArray *myArray" instead of only "id".

还有另一个问题涉及类方法和实例方法之间的区别。


查看完整回答
反对 回复 2019-10-17
?
料青山看我应如是

TA贡献1772条经验 获得超7个赞

(+)代表类方法,(-)代表实例方法,


(+)类方法:-


是声明为静态的方法。可以在不创建类实例的情况下调用该方法。类方法只能对类成员操作,而不能对实例成员操作,因为类方法不知道实例成员。除非在该类的实例上调用它们,否则也不能从该类方法内调用该类的实例方法。


(-)实例方法:-


另一方面,需要先存在该类的实例,然后才能调用它们,因此需要使用new关键字创建一个类的实例。实例方法在类的特定实例上运行。实例方法未声明为静态。


如何创建?


@interface CustomClass : NSObject


+ (void)classMethod;

- (void)instanceMethod;


@end

如何使用?


[CustomClass classMethod];


CustomClass *classObject = [[CustomClass alloc] init];

[classObject instanceMethod];


查看完整回答
反对 回复 2019-10-17
  • 3 回答
  • 0 关注
  • 532 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信