Ram
Ram

Reputation: 2513

What is the difference between (+) and (-) operator in obj c function

Hi i am new to iphone and starts to learning Obj c.

i have notice the function definition, for some function we are using (-) and for some function we are using (+)

Example: + (id)requestWithURL:(NSURL *)theURL

– initWithURL:

what the difference between those two operator / symbol's usage?

thanks!

Upvotes: 1

Views: 231

Answers (2)

LBushkin
LBushkin

Reputation: 131676

In the context of a class definition, +/- determine whether the methods are instance or class level methods.

+ indicates the method is class level, and you don't need an instance to call it.

- indicates the method is an instance method, and must be called through an instance of an object.

A common example of a static (+) method is NSString::stringWithFormat, when you call it, you do so without an instance, but rather using the class name:

[NSString stringWithFormat: @"Your age is %d", age];

An instance method must be called on an instance of an appropriate object, an example of one would be:

NSString *s = @"oop:ack:zonks::ponies";
int len = [s length]; // instance method called

These symbols should not be confused with the mathematical operators + and -, which can only be applied as part of binary or unary arithmetic expressions.

Upvotes: 10

djhworld
djhworld

Reputation: 6776

+ = Static methods (i.e. you don't need an instance of the class to call the method - but you can't use non-static member variables or anything like that)

- = Instance methods

Upvotes: 3

Related Questions