Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 399
Sharief Avatar asked Apr 09 '10 22:04

Sharief


People also ask

What is the And before function in Objective-C?

One way to describe the difference is that - methods operate on objects, while + methods operate on the class itself.

What is the difference between method that begin with and -?

With the class based function, you just call directly, but you don't have access to any local variables besides #defines that you can do because the class isn't instantiated. But with the - (NSString you must instantiate the class before use, and you have access to all local variables.

What does @() mean in Objective-C?

In Objective-C, any character , numeric or boolean literal prefixed with the '@' character will evaluate to a pointer to an NSNumber object (In this case), initialized with that value. C's type suffixes may be used to control the size of numeric literals. '@' is used a lot in the objective-C world.


1 Answers

+ 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]

like image 50
Jacob Relkin Avatar answered Oct 19 '22 06:10

Jacob Relkin