What is the difference between "+" and "-" before the function name interface declaration in an Objective-C program. Example:
在Objective-C程序中,函数名接口声明前的“+”和“-”有什么区别?例子:
- (void)continueSpeaking;
+ (NSArray *)availableVoices;
What's the difference?
有什么区别呢?
1 个解决方案
#1
23
+
defines a class method
定义一个类方法
Class methods belong to the class itself, not instances of the class.
类方法属于类本身,而不是类的实例。
Example: [AppDelegate someMethod]
例如:[AppDelegate someMethod]
-
defines an instance method
-定义实例方法
Example [[[UIApplication sharedApplication] delegate] someMethod]
示例[[[[UIApplication sharedApplication]委托]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
:
假设你的类名为MyClass,你创建了它的一个实例并将它存储到一个名为myInstance的变量中:
- (void)continueSpeaking
can be called like so: [myInstance continueSpeaking]
.
- (void) continuation - speech可这样调用:[我的实例continuation - speech]。
However, the method + (NSArray *)availableVoices
can only be called like so: [MyClass availableVoices]
但是,方法+ (NSArray *)availableVoices只能这样调用:[MyClass availableVoices]
#1
23
+
defines a class method
定义一个类方法
Class methods belong to the class itself, not instances of the class.
类方法属于类本身,而不是类的实例。
Example: [AppDelegate someMethod]
例如:[AppDelegate someMethod]
-
defines an instance method
-定义实例方法
Example [[[UIApplication sharedApplication] delegate] someMethod]
示例[[[[UIApplication sharedApplication]委托]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
:
假设你的类名为MyClass,你创建了它的一个实例并将它存储到一个名为myInstance的变量中:
- (void)continueSpeaking
can be called like so: [myInstance continueSpeaking]
.
- (void) continuation - speech可这样调用:[我的实例continuation - speech]。
However, the method + (NSArray *)availableVoices
can only be called like so: [MyClass availableVoices]
但是,方法+ (NSArray *)availableVoices只能这样调用:[MyClass availableVoices]