Possible Duplicate:
What do the plus and minus signs mean in Objective C next to a method?
What's the difference between using a plus or minus in Objective-C?
For example, most of the time code starts -(void)somethingSomethingelse
, but sometimes it will be +(void)somethingSomethingelse
Thanks!
The leading minus sign (-) tells the Objective-C compiler that the method is an instance method. The only other option is a plus sign (+), which indicates a class method. A class method is one that performs some operation on the class itself, such as creating a new instance of the class.
As noted, the @implementation section contains the actual code for the methods you declared in the @interface section. You have to specify what type of data is to be stored in the objects of this class. That is, you have to describe the data that members of the class will contain.
An Objective-C method declaration includes the parameters as part of its name, using colons, like this: - (void)someMethodWithValue:(SomeType)value; As with the return type, the parameter type is specified in parentheses, just like a standard C type-cast.
Objective-C uses the runtime code compilation Generally, this happens very fast but when the code compilation happens a significant number of times, it becomes measurable. Objective-C is a superset of C and all C functions that you will write in Objective-C will be just as fast.
-
functions are instance functions and +
functions are class (static) functions.
So let's say you have a class called Person
, and the following functions
-(void)doSomething;
+(void)doSomethingElse;
You would invoke these functions with the following:
Person *myPerson = [[Person alloc] init];
[myPerson doSomething];
[Person doSomethingElse];
This is more of a syntax description, assuming you understand the concept of class vs instance.
edit:
just to add: In objective-C, you can actually invoke a class function on an instance, but the effect is no different than invoking it on the class itself (essentially compiles to the same thing).
So you can do
[myPerson doSomethingElse]
Generally, you wouldn't do this as it is confusing and misleading to read. I am pointing it out so you won't be surprised if you come across code like this somewhere.
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