198

In Objective-C, I would like to know what the + and - signs next to a method definition mean.

- (void)loadPluginsAtPath:(NSString*)pluginPath errors:(NSArray **)errors;
Cœur
  • 37,241
  • 25
  • 195
  • 267
gyurisc
  • 11,234
  • 16
  • 68
  • 102

4 Answers4

241

+ is for a class method and - is for an instance method.

E.g.

// Not actually Apple's code.
@interface NSArray : NSObject {
}
+ (NSArray *)array;
- (id)objectAtIndex:(NSUInteger)index;
@end

// somewhere else:

id myArray = [NSArray array];         // see how the message is sent to NSArray?
id obj = [myArray objectAtIndex:4];   // here the message is sent to myArray

// Btw, in production code one uses "NSArray *myArray" instead of only "id".

There's another question dealing with the difference between class and instance methods.

Community
  • 1
  • 1
Georg Schölly
  • 124,188
  • 49
  • 220
  • 267
  • 77
    It's almost as if the extra five characters of "static" are somehow too much for them. – Anon. Jan 19 '10 at 21:40
  • 2
    Link to docs: http://developer.apple.com/mac/library/DOCUMENTATION/Cocoa/Conceptual/ObjectiveC/Articles/ocDefiningClasses.html#//apple_ref/doc/uid/TP30001163-CH12-TPXREF122 – Seth Jan 19 '10 at 21:41
  • 26
    @Anon: The methods aren't static. They are class methods. They can be overridden and are very much a part of the class hierarchy. static implies something very different in C. – bbum Jan 19 '10 at 22:03
  • 5
    @Avon, that's apple for you, they'll leave out a flash on their camera too, and a right button on their mice. =) – pokstad Jan 20 '10 at 00:55
  • 15
    @bbum is on the money. The fact that Java re-appropriated the "static" keyword to mean something different is not the fault of the much-older C. While its usage may not be entirely intuitive in C, it seems even less so in Java. I would expect static to be the opposite of dynamic — something which doesn't change. And of course, the Objective-C language was around for nearly 15 years before Apple adopted it in OS X. – Quinn Taylor May 05 '10 at 04:15
  • I can't seem to memorize which is plus and which is minus. Is there any logic behind the symbol choice? – martinkunev Apr 03 '14 at 13:06
  • @bbum They are static - its about Object Oriented Programming, not the C. – Luten Aug 14 '14 at 05:08
  • @Luten that the question is tagged "objective-c" and OP specifically asks about "objective-c" yields an answer focused on "objective-c". As well, in oop, there is a difference between the two. – bbum Aug 14 '14 at 13:55
  • 3
    The difference between static and a class method: Define the method +foo in MYBaseClass and its subclass MYSubClass. NSArray* classes = @[ [MYBaseClass class], [MYSubClass class] ]; [classes makeObjectsPerformSelector: @selector(foo)]; Wouldn't work with a static method. That said, I would'a preferred an `@classmethod` and `@method` or so, too. Why so terse ... ? – uliwitness Aug 14 '14 at 14:34
  • 2
    @uliwitness The terseness goes way way back to the beginning of time when ObjC was implemented as a preprocessor on top of C compilers. At that time, using `@...` markup beyond introducing class interfaces and implementations was quite uncommon. By the time `@...` became common, `+` vs. `-` was ingrained and, I'd guess, it was never considered worthwhile to add a synonymous syntactic form for method introduction. – bbum Aug 14 '14 at 17:19
  • 1
    @Luten `static` is a far too overridden word in programming. :) – bbum Aug 15 '14 at 20:36
55

(+) for class methods and (-) for instance method,

(+) Class methods:-

Are methods which are declared as static. The method can be called without creating an instance of the class. Class methods can only operate on class members and not on instance members as class methods are unaware of instance members. Instance methods of the class can also not be called from within a class method unless they are being called on an instance of that class.

(-) Instance methods:-

On the other hand require an instance of the class to exist before they can be called, so an instance of a class needs to be created by using the new keyword. Instance methods operate on specific instances of classes. Instance methods are not declared as static.

How to create?

@interface CustomClass : NSObject

+ (void)classMethod;
- (void)instanceMethod;

@end

How to use?

[CustomClass classMethod];

CustomClass *classObject = [[CustomClass alloc] init];
[classObject instanceMethod];
Vandit Mehta
  • 2,572
  • 26
  • 41
19

+ methods are class methods - that is, methods which do not have access to an instances properties. Used for methods like alloc or helper methods for the class that do not require access to instance variables

- methods are instance methods - relate to a single instance of an object. Usually used for most methods on a class.

See the Language Specification for more detail.

Robert Christie
  • 20,177
  • 8
  • 42
  • 37
  • Well, actually class methods do have access to instance variables, they just don't have an instance as `self`, but rather the class. They're simply not associated with an instance, and method look-up is not through the instance, but through the class. Still, you could do `+exchangeIVarOf: (MYObject*)a with: (MYObject*)b { MYObject* x = a->ivar; a->ivar = b->ivar; b->ivar = x; }` – uliwitness Aug 14 '14 at 14:40
5

The definitive explanation of this from Apple is here, under the 'Methods and Messaging' section:

https://developer.apple.com/library/mac/referencelibrary/GettingStarted/RoadMapOSX/books/WriteObjective-CCode/WriteObjective-CCode/WriteObjective-CCode.html

In brief:

+ means 'class method'

(method can be called without an instance of the class being instantiated). So you call it like this:

[className classMethod]; 


- means 'instance method'

You need to instantiate an object first, then you can call the method on the object). You can manually instantiate an object like this:

SomeClass* myInstance = [[SomeClass alloc] init];

(this essentially allocates memory space for the object then initalises the object in that space - an oversimplification but a good way to think about it. You can alloc and init the object seperately but never do this - it can lead to nasty issues related to pointers and memory management)

Then call the instance method:

[myInstance instanceMethod]

An alternative way to get an instance of an object in Objective C is like this:

NSNumber *myNumber = [NSNumber numberWithInt:123];

which is calling the 'numberWithInt' class method of the NSNumber class, which is a 'factory' method (i.e. a method that provides you with a 'ready made instance' of an object).

Objective C also allows the creation of certain object instances directly using special syntax, like in the case of a string like this:

NSString *myStringInstance = @"abc";

Chris Halcrow
  • 28,994
  • 18
  • 176
  • 206