Question

Hi I was studying about initialize class method in objective-C. I came across this code which confused me

+ (void)initialize {
    if (self == [Car class]) // what is the use of method class??
    {
        // Makes sure this isn't executed more than once
        _defaultModel = @"Nissan Versa"; // initializing the static variable  _defaultModel
    }
}

Here I know that we're using if (self == [Car class]) to check whether the self is referring to class itself or not. But what is the functionality of method class in [Car class] . I'm a newbie and I'm learning objective-C from couple of weeks when I searched for answers for this question all i got is difference b/w class methods and instance methods which I know. So please tell me what is the functionality of method class. Thanks in advance .

Was it helpful?

Solution

Two things to note here.

First, as you say the code is "using if (self == [Car class]) to check whether the self is referring to class itself or not". The method class returns the class object for Car, and self will be the same object if initialize is being called on Car.

Second, how could the class method initialize of Car be called on anything other than Car itself - it belongs to the class after all?

The answer to this lies in inheritance. The +initialize of Car will be called once before any instance of Car is created. However it will be also called once for every subclass of Car before the first instance of that subclass is created. E.g. if you have a class:

@interface Volvo : Car
...
@end

Then if Volvo has an +initialize then it will be called before the first instance of Volvo is created and Car's +initialize will also be called before the first instance of Volvo is created. This is why you will often see class initializers with an if statement to check for self being the class itself - it ensures the code is only executed once and not once per subclass as well.

HTH

OTHER TIPS

[NSObject class]

Returns the class type of the object. See Apple's API

(By the way as a side note it's always good to google for the API before asking questions here)

There are two methods named class in Cocoa: the class method +[NSObject class], and the instance method -[NSObject class]. They do very different things, so do not get them mixed up. The former overrides the latter for class objects.

Here you are asking about the class method +[NSObject class], so that's what we will talk about. This method simply returns the object (class object) that it is called on, similar to -[NSObject self]. In fact, [Car class] can also be written as [Car self].

So why do we use a method that simply returns what it was called on? Why not just use the thing it's called on directly? This is due to a peculiarity in Objective-C syntax. In Objective-C, a class name like Car is not a valid expression (don't know why; the gods just made it that way). So we cannot just directly write self == Car, even though that's what we're really trying to say.

On the other hand, Objective-C has a special case for the message sending syntax [ ], where if the thing on the left side is a class name, the message is sent to the class object. So the "work-around" to get the class object for a class is to use the [ ] syntax to call a dummy "return self" method on it. In the Cocoa/GNUStep/OpenStep framework, there are two methods that can do this, +class, and -self. By convention, +class is used (which is really unfortunate, due to confusion with -class). After a while, you'll just get used to seeing [SomeClass class] and interpreting it as the SomeClass class object, whereas [some_obj class] gets the class of some_obj if it is not a class object.

(Note: In Smalltalk, which is where Objective-C inherited much of its stuff from, a class name is the expression for a class. So something like self == Car would be the correct way to write it. And the class method in Smalltalk always gets the class of the object, regardless of whether it's a class object or not.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top