24

Possible Duplicate:
What do the plus and minus signs mean in Objective C next to a method?

What's the difference between using a plus or minus in Objective-C?

For example, most of the time code starts -(void)somethingSomethingelse, but sometimes it will be +(void)somethingSomethingelse

Thanks!

Community
  • 1
  • 1
SnowboardBruin
  • 3,645
  • 8
  • 36
  • 59

3 Answers3

35

- functions are instance functions and + functions are class (static) functions.

So let's say you have a class called Person, and the following functions

-(void)doSomething;

+(void)doSomethingElse;

You would invoke these functions with the following:

Person *myPerson = [[Person alloc] init];

[myPerson doSomething];

[Person doSomethingElse];

This is more of a syntax description, assuming you understand the concept of class vs instance.

edit:

just to add: In objective-C, you can actually invoke a class function on an instance, but the effect is no different than invoking it on the class itself (essentially compiles to the same thing).

So you can do

[myPerson doSomethingElse]

Generally, you wouldn't do this as it is confusing and misleading to read. I am pointing it out so you won't be surprised if you come across code like this somewhere.

Dima
  • 23,484
  • 6
  • 56
  • 83
30

In short, (+) is a class method and (-) is an instance method

See this answer for a full explanation What is the difference between class and instance methods?

Community
  • 1
  • 1
Scott Bossak
  • 2,471
  • 20
  • 17
6

member and public functions respectively.

Such that

id object = [[NSObject alloc] init];
+ (id)alloc;
- (id)init;

Where NSObject is a Class and id is an object

If you have ever used C++, a + is equivalent to static

Nico
  • 3,826
  • 1
  • 21
  • 31