plus (+) versus minus (-) in objective-c [duplicate] plus (+) versus minus (-) in objective-c [duplicate] ios ios

plus (+) versus minus (-) in objective-c [duplicate]


- 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.


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

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


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