“+”之间的区别和“ - ”在Objective-C中的函数名之前 [英] Difference between "+" and "-" before function name in Objective-C
问题描述
- (void)continueSpeaking;
+(NSArray *)availableVoices;
有什么区别?
类方法属于类本身,而不是实例。
示例: [AppDelegate someMethod] $ c
$ -
定义一个实例方法
示例 [[[UIApplication sharedApplication] delegate] someMethod]
描述差异的一种方法是 -
方法对对象操作,而 +
方法对类本身进行操作。
假定你的类被命名为 MyClass
,并且你创建了它的一个实例并将它存储到一个名为 myInstance
:
$ b - (void)continueSpeaking
可以这样调用: [myInstance但是,方法
+(NSArray *)avai
[MyClass availableVoices]
What is the difference between "+" and "-" before the function name interface declaration in an Objective-C program. Example:
- (void)continueSpeaking;
+ (NSArray *)availableVoices;
What's the difference?
+
defines a class method
Class methods belong to the class itself, not instances of the class.
Example: [AppDelegate someMethod]
-
defines an instance method
Example [[[UIApplication sharedApplication] delegate] someMethod]
One way to describe the difference is that -
methods operate on objects, while +
methods operate on the class itself.
Say your class was named MyClass
, and you created an instance of it and stored it into a variable called myInstance
:
- (void)continueSpeaking
can be called like so: [myInstance continueSpeaking]
.
However, the method + (NSArray *)availableVoices
can only be called like so: [MyClass availableVoices]
这篇关于“+”之间的区别和“ - ”在Objective-C中的函数名之前的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!