38

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

e.g

- (void)methodname

+ (void)methodname
Jasarien
  • 58,279
  • 31
  • 157
  • 188
Alex
  • 2,513
  • 5
  • 27
  • 42

5 Answers5

57

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
  • 58,279
  • 31
  • 157
  • 188
  • 1
    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 Feb 12 '10 at 13:44
  • 2
    I can't comment on the choice of notation, because I simply don't know... Sorry. Maybe someone else will know? – Jasarien Feb 12 '10 at 13:55
  • 2
    UML uses +/- sure. Also UML uses +/- to annotate methods. However other than that UML's use and Obj-C use are completely different. – deft_code Sep 03 '10 at 21:16
  • @FrançoisBeausoleil I think uml uses +/- signs to for public/private attribute or functions. (http://www.tutorialspoint.com/images/notation_class.jpg) – Mehdi Karamosly Oct 22 '13 at 19:33
7

+(void)methodname is a class variable and -(void)methodname is object variable.

Lets say you make a utility class that has a method to reverse string. The class you call MYUtility.

If you use +, like

+ (NSString *)reverse:(NSString *)stringToReverse

You could use it directly like

NSString *reversed = [MYUtility stringToReverse:@"I Love objective C"];

if you used a -, like

- (NSString *)reverse:(NSString *)stringToReverse

You have to use :

MYUtility *myUtil = [[MYUtility alloc] init];
NSString *reversed = [myUtil stringToReverse:@"There are many ways to do the same thing"];

With the class based function, you just call directly, but you don't have access to any local variables besides #defines that you can do because the class isn't instantiated.

But with the - (NSString you must instantiate the class before use, and you have access to all local variables.

This isn't a pick one thing and stay with it, many classes have both, just look at the header file for NSString, it is littered with + functions and - functions.

Abhinay Reddy Keesara
  • 9,763
  • 2
  • 18
  • 28
2

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
  • 13,228
  • 36
  • 108
  • 159
1

According to this page:

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

See this SO question for more information.

Community
  • 1
  • 1
Kaleb Brasee
  • 51,193
  • 8
  • 108
  • 113
0

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.

Stefan Arentz
  • 34,311
  • 8
  • 67
  • 88