user2485710
user2485710

Reputation: 9801

What is a class object in Objective-C 2.0?

In the official documentation for Objective C 2.0 titled The Objective-C 2.0 Programming Language from Apple, released in 2009, there is a paragraph about Class Objects on page 28.

I don't understand what Class Objects are, and how to define them aside from the rest of the language and what properties they have. In the same document it's explained that everything in Objective-C 2.0 is an object, this object is basically a pointer to a struct that contains an isa field and the pointer itself is of type id. From this I'm deducing that:

Assuming that I got how Objective C 2.0 works, what is a class object and how is it different from the way instances are created? What kind of properties does a class object offer that an instance doesn't have ? Can you make a parallel with C or C++?

Upvotes: 6

Views: 404

Answers (3)

Tricertops
Tricertops

Reputation: 8512

Object is a structure, that has isa field pointing to its Class. This isa allows the structure to receive Objective-C messages, which makes it an Object. Class pointed by isa is used to lookup implementations for these messages.

(Interestingly, also blocks are objects and GCD structures are too. They both can receive messages, like -copy for blocks or -description for dispatch_queue.)

Class is a structure used to look-up methods of its instances. It has a list of method implementations for their instances (-methods). Classes have an isa field, so they qualify as Objects, thus can also receive messages. isa of Class points to a Metaclass, so a Class is instance of a Metaclass. Its single instance – a singleton.

Metaclass is where I'm getting lost, but it's definitely an Object, because it has isa. Metaclass has a list of method implementations (+methods) of its single instance – the Class.

When you write this code:

@interface MYObject : NSObject
+ (void)classMethod; // Stored in Metaclass
- (void)instanceMethod; // Stored in Class
@end

You are creating a pair: Class and Metaclass.

Oh, and what is a class of Metaclass? A Root Metaclass!
And what is a class of the Root Metaclass? The Root Metaclass itself!
But then where isa of Metaclasses points to? To our old friend NSObject. Too meta, right?

Upvotes: 1

Chuck
Chuck

Reputation: 237060

OK, so you define a class. We'll call him Charlie:

@interface Charlie : NSObject
@end

There's our little class! Since — like every other class in Obective-C — Charlie is an object, you can send it messages like [Charlie alloc] to have Charlie allocate an instance for you. This is what we mean by a class object: It's the object that represents the class you defined.

What is an object in Objective-C? Classes are defined based on this struct:

struct objc_object {
    Class isa  OBJC_ISA_AVAILABILITY;
};

And a class is represented by a struct like this:

struct objc_class {         
    struct objc_class *isa; 
    struct objc_class *super_class; 
    // A bunch of other members …
}

As you can see, the both start with an isa referring to a class. So a class is just an extension of normal objects.

When Charlie creates an instance, that instance's isa will point to Charlie. But what does Charlie's isa point to? Well, it points to a metaclass. A metaclass is a strange thing — it's a special kind of class that exists just to act as a class's class. You never interact with it directly; it just does its classly duties† when you interact with its sole instance, Charlie.

So that's what we mean when we talk about a class object — it's just the object that represents the class you defined in code.

† You might be wondering what a class's duties are. Well, the obvious biggie is that it's how you create your objects. But besides that, instances in Objective-C do not hold their own methods. Instead, method resolution is done based on an object's isa, so the class's most important function, besides creating instances for you, is determining what methods your object has.

Upvotes: 5

Merlevede
Merlevede

Reputation: 8170

what is a class object and how it's different from the way instances are created ? What kind of properties a class object offers that an instance doesn't have ? Can you make a parallel with C or C++ ?

Let's try to compare with C and C++. First there is no comparison to C, because C is not object oriented, so the concept of object or class does not exist. In C++ you have classes (or objects) which you declare in your .h file, there you write the definition of the class (the name, the instance variables, and methods or functions), and then in your .cpp file you implement the methods declared in the definition.
Also in C++ you can have static variables and methods, which, as you probably know, don't belong to a specific instance of the class, we could say that they affect all instances.

In objective C, a class property or a class method is analogous to the static variables and static funcions in C++.

A class object is the way objective C encapsulates the definition of a class and makes it available at runtime. You don't necessarily instantiate class objects explicitly.

Consider this class

@interface MyObject : NSObject
{
    int i;
}
- (void)myFunction;
+ (void)classFunction;
@end

You can instantiate such an object using:

MyObject *obj = [[MyObject alloc] init];

Here you're using the alloc method of the MyObject class object. Something important to understand is that you don't instantiate class Objects, the compiler creates just one object, a class object, to represent the class.

Upvotes: 2

Related Questions