views:

82

answers:

2

What's the difference between - and + when declaring with Cocoa/Obj-C.

e.g. -(void)doSomething{} or +(void)doSomething{}

+11  A: 

"-" means an instance method (meaning relative to a specific object), while "+" means a class method (meaning usable by a class in general—it doesn't have to be tied to a specific object).

Example:

For "- (void)doSomething":

MyClass *obj = [[MyClass alloc] init];
[obj doSomething];

vs

[MyClass doSomething];

for "+ (void)doSomething".

htw
+3  A: 

Just to add a little analogy to htw's answer:

  • + is for what a static method would be in C++, C# or Java
  • - is for what a non-static (regular) method would be in C++, C# of Java
Pablo Santa Cruz