PDA

View Full Version : +/ - Functions




Nnavick
Apr 1, 2011, 10:13 AM
what is the different between

+(void)blabla;

and

-(void)blabla;

what the - and + do?


thanks!



balamw
Apr 1, 2011, 10:17 AM
Class vs. instance methods. http://stackoverflow.com/questions/1053592/objective-c-class-vs-instance-methods

Covered in any general book on Objective-C/Cocoa/Cocoa Touch.

B

Sykte
Apr 2, 2011, 08:56 AM
what is the different between

+(void)blabla;

and

-(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.

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

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


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

seepel
Apr 2, 2011, 02:51 PM
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


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


vs


- (id)initWithEngine:(Engine *)engine;