views:

178

answers:

4

What is the difference between methods that are declared with - and methods that are declared with +

e.g

- (void)methodname

+ (void)methodname
+1  A: 

According to this page:

Instance methods begin with - and class level methods begin with +

See this SO question for more information.

Kaleb Brasee
+1  A: 

minus are instance methods (only accessible via an instantiated object)

plus are class methods (like in Java Math.abs(), you can use it without an instantited object)

Tim
A: 

The first is an instance method and the second is a class method. You should read Apple's Objective-C documentation to learn about the difference.

St3fan
+12  A: 

Methods prefixed with - are instance methods. This means they can only be invoked on an instance of a class, eg:

[myStringInstance length];

Methods prefixed with + are class methods. This means they can be called on Classes, without needing an instance, eg:

[NSString stringWithString:@"Hello World"];
Jasarien
Just curious: why this choice of notation? +/- feels a bit hackish to me (probably because it reminds me of how perl uses '@', '#', '$' to mean *something*)
Anthony Kong
I can't comment on the choice of notation, because I simply don't know... Sorry. Maybe someone else will know?
Jasarien
UML uses the same notation.
François Beausoleil
UML uses +/- sure. Also UML uses +/- to annotate methods. However other than that UML's use and Obj-C use are completely different.
caspin