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?
One way to describe the difference is that - methods operate on objects, while + methods operate on the class itself.
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.
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.
+
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With