preface

Let’s start with apple’s official ISA location and inheritance chart.

The metaclass

1. Find metaclasses

First find metaclass, first check LQTeacher address, through LQTeacher address to find LQTeacher ISA, through ISA to get the pointer stored inside, get a new address, Po view found is LQTeacher class, this is our metaclass!

2. To summarize

  • Metaclasses are automatically created by the system and have the same name as the associated class.
  • The ISA of an object points to a class, and the ISA of a class object points to a metaclass

Class and metaclass inheritance

1. No more nonsense, first on the code! The code creates two classes, an LQPerson and a LGTeacher, LQTeacher inherits LQPerson. Then test the class and metaclass inheritance separately!

#import <Foundation/Foundation.h> #import <objc/runtime.h> #import <Foundation/Foundation.h> @interface LQPerson : NSObject @end @implementation LQPerson @end @interface LQTeacher : LQPerson @end @implementation LQTeacher @end #pragma mark - pragma mark void testSuperClass(id){class CLS = class; Class superCls = class_getSuperclass(cls); Class rootSuperClass = class_getSuperclass(superCls); NSLog (@ "class: % @ _ % p", CLS, CLS); NSLog (@ "parent class: % @ _ % p", superCls, superCls); NSLog (@ "parent class: % @ _ % p", rootSuperClass, rootSuperClass); NSLog(@"----------"); } #pragma mark - pragma mark - pragma mark - pragma mark Class metaClass = object_getClass(cls); NSLog (@ "class: % @ _ % p", CLS, CLS); NSLog (@ "metaClass: % @ _ % p", the metaClass, metaClass). testSuperClass(metaClass); } int main(int argc, const char * argv[]) {@autoreleasepool {NSLog(@" class inheritance :"); testSuperClass(LQTeacher.class); testSuperClass(LQPerson.class); testSuperClass(NSObject.class); NSLog(@" Metaclass inheritance probe :"); testMetaClass(LQTeacher.class); testMetaClass(LQPerson.class); testMetaClass(NSObject.class); } return 0; }Copy the code

2. Test the running result

3. Summary

Class inheritance diagram and metaclass inheritance diagram

ISA walk a

Test cases

#pragma mark-isa (id obj){Class ISA = object_getClass(obj); #pragma mark-isa (id obj){Class ISA = object_getClass(obj); Class metaIsa = object_getClass(isa); Class rootMetaIsa = object_getClass(metaIsa); NSLog (@ "object: % @ _ % p", obj, obj); NSLog(@" object isa-->%@_%p",isa,isa); NSLog (@ "class isa - > % @ _ % p", metaIsa, metaIsa); NSLog (@ "metaclass isa - > % @ _ % p", rootMetaIsa, rootMetaIsa); NSLog(@"----------"); } int main(int argc, const char * argv[]) {@autoreleasepool {NSLog(@"ISA inheritance :"); NSLog (@ "LQTeacher classes: % @ _ % p", LQTeacher. Class, LQTeacher. Class); NSLog (@ "LQPerson classes: % @ _ % p", LQPerson. Class, LQPerson. Class); NSLog (@ "NSObject class: % @ _ % p", NSObject. Class, NSObject. Class); NSLog(@"----------"); testSuperIsa(LQTeacher.new); testSuperIsa(LQPerson.new); testSuperIsa(NSObject.new); } return 0; }Copy the code

2. Test results

3. Summary

  1. Object’s ISA points to a class
  2. Class ISA points to the metaclass
  3. The ISA of the metaclass points to the root metaclass
  4. The ISA of the root metaclass points to itself