The following figure shows the relationship between instance objects, class objects, and metaclasses, where the implementation arrow represents the parent class relationship and the dotted line represents the pointing relationship of ISA (ISA can be understood as the parent class, described in the next chapter).

Seeing the results obtained in the figure above, the following steps are introduced:

Step 1: Create the LSPerson class, inherited from NSObject, and create the LSStudent class, inherited from LSPerson

NSObject, LSPerson, LSStudent isa, LSStudent ISA, NSObject isa, LSStudent ISA, LSStudent ISA, LSStudent ISA, LSStudent ISA, LSStudent ISA, LSStudent ISA, LSStudent ISA, LSStudent ISA, LSStudent ISA, LSStudent ISA, LSStudent ISA, LSStudent ISA, LSStudent ISA, LSStudent ISA

To explore the process

First create the LSPerson class, inherited from NSObject, and create the LSStudent class, inherited from LSPerson, and then get their ISA, respectively

LSPerson *per = [LSPerson alloc]; LSStudent *stu = [LSStudent alloc]; / / objectCopy the code

Gets the OBJECT’s ISA

There are three ways to get the ISA of an object, in the case of LSPerson’s object PER

[per class];
[LSPerson class];
object_getClass(per);
Copy the code

However, to obtain a metaclass isa, you can only use the third method, refer to the source code of the class and object methods, as shown below

+ (Class)class {
    return self;
}
- (Class)class {
    return object_getClass(self);
}

Copy the code

Because you get the class object once, you can’t get the metaclass from the class. You can only get the metaclass from the object_getClass method

Gets the Class of the parent Class

The process of obtaining ISA is the same. Taking LSPerson’s object PER as an example, there are also three kinds, all of which can be obtained

[per superclass];
[LSPerson superclass];
class_getSuperclass(per);
Copy the code

Superclass source code as follows, are to obtain the superclass superclass parent class

+ (Class)superclass { return self->superclass; } - (Class)superclass { return [self class]->superclass; } Class class_getSuperclass(Class cls) { if (! cls) return nil; return cls->superclass; }Copy the code

Get LSPerson, LSStudent, NSObject Class, metaclass, root Class, isa of root Class, and Class of parent Class, respectively

Let’s go straight to the code

LSPerson *per = [LSPerson alloc]; LSStudent *stu = [LSStudent alloc]; Object Class obj = [NSObject Class]; // Class obj1 = object_getClass(obj); // Class obj2 = object_getClass(obj1); // Class obj3 = object_getClass(obj2); / / root metaclass isa NSLog (@ "obj: % % % % p - p - p - p", obj, obj1, obj2, obj3); Class per1 = object_getClass(per); // Class per2 = object_getClass(per1); // Class per3 = object_getClass(per2); // The root metaclass Class per4 = object_getClass(per3); / / root metaclass isa NSLog (@ "per: % % % % p - p - p - p", per1 fires our, per2, these forms, per4); Class stu1 = object_getClass(stu); // Class stu2 = object_getClass(stu1); // Class stu3 = object_getClass(stu2); // The root metaclass Class stu4 = object_getClass(stu3); / / root metaclass class NSLog (@ "stu: % % % % p - p - p - p", stu1, stu2, stu3, stu4); Class supPer1 = class_getSuperclass([per class]); // NSObject Class supPer2 = class_getSuperclass(supPer1); NSObject nil Class supPer3 = class_getSuperclass(STU1); //NSObject nil Class supPer3 = class_getSuperclass(STU1); // Class supPer4 = object_getClass(supPer1); // NSObject metaclass Class supPer5 = class_getSuperclass(supPer3); NSObject NSLog(@"supPer: %p--%p--%p--%p--%p --%p--%p", supPer1, supPer2, supPer3, supPer4, supPer5);Copy the code

The output is as follows

Obj: 0x7fff962b6118-- 0x7ffF962b60f0 -- 0x7ffF962b60f0 --0x7fff962b60f0 per: 0 x100008490-0 x7fff962b60f0 x1000084b8-0-0 x7fff962b60f0 stu: 0 x100008508-0 x7fff962b60f0 x1000084e0-0-0 x7fff962b60f0 supPer: 0x7fff962b6118--0x0--0x100008490--0x7fff962b60f0--0x7fff962b6118Copy the code

You can see that by looking at the results

NSObject’s class and metaclass address are different. The metaclass isa and metaclass address are the same, that is, the metaclass ISA points to itself, and NSObject’s metaclass is the root class

The LSPerson and LSStudent cases show that the metaclass address of the LSPerson and LSStudent classes is not the same as that of NSObject, and the root class is the same as that of NSObject

Super tells you that NSObject’s parent is nil, and the final parent of the class is NSObject, and the final parent of the metaclass is the root class, and the parent of the root class is NSObject

A class may have multiple objects, including only one class and only one metaClass