-1

Possible Duplicate:
Objective-C: Class vs Instance Methods?
Objective-C - difference between class method and static method?

In ObjC, A single dash before a method name means it's a instance method. A plus before a method name means it's a class method. but what is the difference in programming?

Community
  • 1
  • 1
chowfun
  • 1
  • 2

3 Answers3

2

The difference between a class method and an instance method is that an instance method requires an instance of the class on which it will (generally) operate. The message to invoke an instance method must be sent to an instance of a class.

Probably the most common single use of class methods is object factories; messages that you send to a class to create an instance configured according to the parameters you've sent in. For example in Cocoa the NSString class has several class methods named stringWithSomethingOrOther: that will create a new NSString object and hand it back to you.

On the other hand, NSString also has many instance methods - operations which really have no meaning without an actual instance to work with. A commonly-used one might be the length method, which tells you how many characters are in the specific NSString instance to which the message is sent.

Also see this. What is the difference between class and instance methods?

Community
  • 1
  • 1
Angel
  • 902
  • 8
  • 16
0

An instance method is invoked on objects. A class method is invoked on class.

For example the line:

SomeClass *object = [[SomeClass alloc] init];

Here you can see that the "alloc" works on "SomeClass" and not on "object".

Whereas: [object callMyFunction]; will act on "object" and not "class". This is an instance method.

Aniket Inge
  • 25,375
  • 5
  • 50
  • 78
0

The main difference with those two is the former one ie with single dash before it is only called by the instance of that class where it is declared ie one have to create one instance of that class means one object for that class and using . one can call the instance method

In class method, the later one can be called directly using the class name. To call class methods one dosen't need any object.

Please refer this link from apple developers documents

The iOSDev
  • 5,237
  • 7
  • 41
  • 78