Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Objective-C, when should I use class methods and when should I use instance methods?

What is the difference between class and instance methods in Objective-C and when should I use each of them?

like image 531
Tirth Avatar asked Jun 30 '10 05:06

Tirth


People also ask

What is the difference between an instance method and class method?

Instance methods need a class instance and can access the instance through self . Class methods don't need a class instance. They can't access the instance ( self ) but they have access to the class itself via cls .

When would you use a class method?

You can use class methods for any methods that are not bound to a specific instance but the class. In practice, you often use class methods for methods that create an instance of the class. When a method creates an instance of the class and returns it, the method is called a factory method.

Why do we need instance method?

Instance method can access the instance methods and instance variables directly. Instance method can access static variables and static methods directly. Static methods can access the static variables and static methods directly. Static methods can't access instance methods and instance variables directly.

What is the difference between Classmethod and Staticmethod?

Class method can access and modify the class state. Static Method cannot access or modify the class state. The class method takes the class as parameter to know about the state of that class. Static methods do not know about class state.


2 Answers

Using the tired old Car analogy...

Think of a Class like it is a factory that makes Instances of the class. For example, you might have a Car class and you might declare a method like:

+ carWithColor: (NSColor *) aColor;

And that method would then create a new Car instance, set the color, and return it:

 + carWithColor: (NSColor *) aColor
 {
     Car *aCar = [[[self alloc] init] autorelease];
     [aCar paintWithColor: aColor];
     return aCar;
 }

Now, that Car class would then declare an instance method that allows the car to be painted. Why an instance method? Because every car can have a different color (and the color of the car would likely be stored in an instance variable).

- (void) paintWithColor: (NSColor *) aColor
{
    ... do your paint stuff here ...
}

This is explained in the Objects, Classes, and Messaging section of the Objective-C documentation.

like image 64
bbum Avatar answered Nov 13 '22 12:11

bbum


This is an old post, but since it comes up first in a Google search I thought I'd add to it.

I'm not going to talk about class methods used as factory methods. I'd like to talk about their use in utility methods. You can/should use class methods for utility methods that are independent of state. What does this mean? Well, for instance, if you're formatting a date the same way for all instances, that's a utility method that should be a class method. Think of the utility method like a screw driver. You don't need to make a new instance of the screw driver every time you want to do something with it. The screw driver remains constant. So, for instance, I have a class that includes a private method that generates a string of emDashes used for displaying to the view. This method is not dependent on state and hence will not vary by instance. Think of class utility methods like constants.

+ (NSString *)emDashString {
return @"   \u2014 \u2014 \u2014 \u2014 \u2014 \u2014 \u2014 \u2014 \u2014";

}

You can call this method generically within the class (it's private in my example) like this:

NSString *string = [[self class] emDashString ];

I've deliberately chosen a bit of a trivial example to drive the point home. You would only bother making this a class utility method if you're going to need this string more than once in your class. Notice that instead of referring to the class by name I call it generically with [self class] since this is called internally. If it's exposed and you want to call it from another class then refer to it by the class name as usual.

like image 32
smileBot Avatar answered Nov 13 '22 14:11

smileBot