Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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!

like image 600
SnowboardBruin Avatar asked Jun 08 '12 18:06

SnowboardBruin


People also ask

What is plus and minus in Objective-C?

The leading minus sign (-) tells the Objective-C compiler that the method is an instance method. The only other option is a plus sign (+), which indicates a class method. A class method is one that performs some operation on the class itself, such as creating a new instance of the class.

What is @implementation in Objective-C?

As noted, the @implementation section contains the actual code for the methods you declared in the @interface section. You have to specify what type of data is to be stored in the objects of this class. That is, you have to describe the data that members of the class will contain.

How do you declare a method in Objective-C?

An Objective-C method declaration includes the parameters as part of its name, using colons, like this: - (void)someMethodWithValue:(SomeType)value; As with the return type, the parameter type is specified in parentheses, just like a standard C type-cast.

Is Objective-C as fast as C?

Objective-C uses the runtime code compilation Generally, this happens very fast but when the code compilation happens a significant number of times, it becomes measurable. Objective-C is a superset of C and all C functions that you will write in Objective-C will be just as fast.


1 Answers

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

like image 180
Dima Avatar answered Oct 26 '22 01:10

Dima