开发者

Difference between "+" and "-" before function name in Objective-C

开发者 https://www.devze.com 2022-12-26 21:35 出处:网络
What is the diffe开发者_如何学Gorence between \"+\" and \"-\" before the function name interface declaration in an Objective-C program.Example:

What is the diffe开发者_如何学Gorence 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]

0

精彩评论

暂无评论...
验证码 换一张
取 消