Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a class object in Objective-C 2.0?

Tags:

objective-c

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:

  • inheritance in Obj-C 2.0 basically consists in chaining those struct through the id and isa field
    • objects that are superclass construct the isa field in a way that it points to a nil object.
  • id is a valid datatype for pretty much everything in Objective C 2.0
  • when defining a class, everything that defines the class itself ( methods and variables ) is packed starting from after/below the isa pointer

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++?

like image 744
user2485710 Avatar asked Feb 28 '14 20:02

user2485710


People also ask

What is class object in C?

A class is used to specify the form of an object and it combines data representation and methods for manipulating that data into one neat package. The data and methods within a class are called members of the class.

What is a class object?

A class is a user-defined type that describes what a certain type of object will look like. A class description consists of a declaration and a definition. Usually these pieces are split into separate files. An object is a single instance of a class. You can create many objects from the same class type.

How do you define an object in Objective-C?

In object-oriented programming terms, an object is an instance of a class. This chapter demonstrates how to define classes in Objective-C by declaring an interface, which describes the way you intend the class and its instances to be used.

What is class object in OOP?

Classes are user-defined data types that act as the blueprint for individual objects, attributes and methods. Objects are instances of a class created with specifically defined data. Objects can correspond to real-world objects or an abstract entity.


2 Answers

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.

like image 154
Chuck Avatar answered Sep 28 '22 10:09

Chuck


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.

like image 43
Merlevede Avatar answered Sep 28 '22 09:09

Merlevede