N Nnavick macrumors regular Original poster Oct 13, 2010 100 0 Apr 1, 2011 #1 what is the different between Code: +(void)blabla; and Code: -(void)blabla; what the - and + do? thanks!
what is the different between Code: +(void)blabla; and Code: -(void)blabla; what the - and + do? thanks!
balamw Moderator emeritus Aug 16, 2005 19,365 980 New England Apr 1, 2011 #2 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
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
S Sykte macrumors regular Aug 26, 2010 223 0 Apr 2, 2011 #3 Nnavick said: what is the different between Code: +(void)blabla; and Code: -(void)blabla; what the - and + do? thanks! Click to expand... - 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: Apr 2, 2011
Nnavick said: what is the different between Code: +(void)blabla; and Code: -(void)blabla; what the - and + do? thanks! Click to expand... - 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
seepel macrumors 6502 Dec 22, 2009 471 1 Apr 2, 2011 #4 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;
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;