Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Nnavick

macrumors regular
Original poster
Oct 13, 2010
100
0
what is the different between

Code:
+(void)blabla;

and

Code:
-(void)blabla;

what the - and + do?


thanks!
 

Sykte

macrumors regular
Aug 26, 2010
223
0
what is the different between

Code:
+(void)blabla;

and

Code:
-(void)blabla;

what the - and + do?


thanks!

- sends the message (method) to a particular instance of your class (object), whereas the + sends the message to the class itself.

So an example of a class method (+) would be with NSString.

Code:
[NSString stringWithString:@"test"]; //this is a class method

An example of an instance method (-) with NSString.

Code:
NSString *mystring = [[NSString alloc] initWithString:@"test"];  //creating my instance
[mystring compare:@"test"]; //this is an instance method
 
Last edited:

seepel

macrumors 6502
Dec 22, 2009
471
1
If you're coming from another programming language like C++ you can kind of think of the (+) functions as static member functions. And they are often used as a pseudo factory methods. For example a car class

Code:
+ (id)carWithEngine:(Engine *)engine;

vs

Code:
- (id)initWithEngine:(Engine *)engine;
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.