简体   繁体   中英

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!

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.

+ = Static methods (ie 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM