Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
We know that every class has its inheritance. Who does that metaclass inherit from? And who would its parent be? Let’s explore this with some simple code. I’m going to create a class LGPerson that inherits from NSObject, so who’s the parent of the LGPerson metaclass? First let’s print the ISA bitmap of NSObject
NSObject *object1 = [NSObject alloc]; // NSObject Class Class = object_getClass(object1); // NSObject metaClass = object_getClass(Class); // NSObject rootMetaClass Class rootMetaClass = object_getClass(metaClass); // NSObject rootMetaClass Class rootRootMetaClass = object_getClass(rootMetaClass); NSLog (@ "\ n \ n % p % p instance objects class p metaClass \ n \ n % % p root metaClass \ n % p spikes metaClass", object1, class, metaClass, rootMetaClass, rootRootMetaClass); -------------------------- // The following output is displayed: 0x100792150 Instance object 0x7FFF807C2088 Class 0x7FFF807C2060 metaclass 0x7FFF807C2060 Root metaclass 0x7FFF807C2060 Root metaclass 0x7FFF807C2060Copy the code
Print the parent of the LGPerson metaclass
// LGPerson Class pMetaClass = object_getClass(lgPerson.class); // the parent of the LGPerson metaclass Class psuperClass = class_getSuperclass(pMetaClass); NSLog(@"%@ - %p",psuperClass,psuperClass); -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - / / output NSObject - 0 x7fff807c2060Copy the code
- Found by the output address,
LGPerson
The parent of a metaclass is notNSObject
, butNSObject
The root metaclass
Can we conclude that the parent of a metaclass is the root metaclass? We use a deeper inheritance chain to verify. Create LGTeacher inheriting from LGPerson, and LGPerson inheriting from NSObject, and print the inheritance chain of LGTeacher. First print the ISA bitmap for LGPerson
Class pMetaClass = object_getClass(lgPerson.class); Class pRootMetaClass = object_getClass(pMetaClass); NSLog (@ "% p LGPerson metaclass \ n % p LGPerson root metaclass", pMetaClass, pRootMetaClass); -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- / / output is: 0 x100008338 LGPerson metaclass 0 x7fff807c2060 LGPerson metaclassCopy the code
Print the parent of LGteacher metaclass
Class tMetaClass = object_getClass(lgteacher.class); LGTeacher -> LGPerson -> NSObject Class tsuperClass = class_getSuperclass(tMetaClass); NSLog(@"%@ - %p",tsuperClass,tsuperClass); -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- / / output is: LGPerson x100008338 0Copy the code
- found
LGTeacher
The parent of a metaclass, pointing to its parentLGPerson
The metaclass
Conclusion:
- It is wrong to conclude that the parent of a metaclass is the root metaclass
- The parent of its metaclass inherits from its parent’s metaclass, which inherits from its root metaclass
The NSObject? And what happens? Let’s print out the root class and the root metaclass
// NSObject Root Class special case Class nsuperClass = class_getSuperclass(nsobjject); NSLog(@"%@ - %p",nsuperClass,nsuperClass); // root metaClass -> NSObject Class = class_getSuperclass(metaClass); NSLog(@"%@ - %p",rnsuperClass,rnsuperClass); -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- / / output results as follows: (null) - 0 x0 NSObject x7fff807c2088 0Copy the code
It can be concluded that:
NSObject
As the root class, there is no parent, or the parent is nil- The parent of the root metaclass refers to the root class, that is:
NSObject
So we can sum it up like this:
- Root class inheritance chain: root class -> nil
- Inheritance chain of the root metaclass: root metaclass -> root class -> nil
We can get the following inheritance relationship:
- Inheritance chain of classes: class -> parent -> root -> nil
- Inheritance chain of metaclass: metaclass -> parent metaclass -> root metaclass -> root class -> nil
- Inheritance relationships come only from classes, not objects
NSObject
As the root class, it wipes the true Lord of all things from which all classes are derivedNSObject