本文介绍了Objective-c中+和 - 方法之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用 - 声明的方法和用+

What is the difference between methods that are declared with - and methods that are declared with +

例如

- (void)methodname

+ (void)methodname

$ b b

推荐答案

- 为前缀的方法是实例方法。这意味着它们只能在类的实例上调用,例如:

Methods prefixed with - are instance methods. This means they can only be invoked on an instance of a class, eg:

[myStringInstance length];

+ 为前缀的方法是类方法。这意味着它们可以在类上调用,而不需要一个实例,例如:

Methods prefixed with + are class methods. This means they can be called on Classes, without needing an instance, eg:

[NSString stringWithString:@"Hello World"];

这篇关于Objective-c中+和 - 方法之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 06:59