title: NSObject isa
一直对NSObject isa理解不是很好,看到个大牛写的博客后,理解很多,在此记录下学习成果,方便日后查看。博客地址 http://xiongzenghuidegithub.github.io/blog/2016/05/17/runtime-part7-object-getclass-yu-object-class-de-qu-bie/
isa指向:对象isa->类,类isa->元类,元类isa->根元类,根元类isa->根元类自己,形成回路
super class:对象super class指向对象super class。元类super class指向super class父类。根元类meta root class指向类NSObject
// 对象
@interface NSObject <NSObject> {
Class isa OBJC_ISA_AVAILABILITY; // 指向类
}
// 类
struct objc_class {
Class isa OBJC_ISA_AVAILABILITY; // 指向元类
#if !__OBJC2__
Class super_class OBJC2_UNAVAILABLE;
const char *name OBJC2_UNAVAILABLE;
long version OBJC2_UNAVAILABLE;
long info OBJC2_UNAVAILABLE;
long instance_size OBJC2_UNAVAILABLE;
struct objc_ivar_list *ivars OBJC2_UNAVAILABLE;
struct objc_method_list **methodLists OBJC2_UNAVAILABLE;
struct objc_cache *cache OBJC2_UNAVAILABLE;
struct objc_protocol_list *protocols OBJC2_UNAVAILABLE;
#endif
} OBJC2_UNAVAILABLE;
// 元类
struct objc_class {
Class isa OBJC_ISA_AVAILABILITY; // 指向root元类
#if !__OBJC2__
Class super_class OBJC2_UNAVAILABLE;
const char *name OBJC2_UNAVAILABLE;
long version OBJC2_UNAVAILABLE;
long info OBJC2_UNAVAILABLE;
long instance_size OBJC2_UNAVAILABLE;
struct objc_ivar_list *ivars OBJC2_UNAVAILABLE;
struct objc_method_list **methodLists OBJC2_UNAVAILABLE;
struct objc_cache *cache OBJC2_UNAVAILABLE;
struct objc_protocol_list *protocols OBJC2_UNAVAILABLE;
#endif
} OBJC2_UNAVAILABLE;
// root元类
struct objc_class {
Class isa OBJC_ISA_AVAILABILITY; // 指向自己
#if !__OBJC2__
Class super_class OBJC2_UNAVAILABLE;
const char *name OBJC2_UNAVAILABLE;
long version OBJC2_UNAVAILABLE;
long info OBJC2_UNAVAILABLE;
long instance_size OBJC2_UNAVAILABLE;
struct objc_ivar_list *ivars OBJC2_UNAVAILABLE;
struct objc_method_list **methodLists OBJC2_UNAVAILABLE;
struct objc_cache *cache OBJC2_UNAVAILABLE;
struct objc_protocol_list *protocols OBJC2_UNAVAILABLE;
#endif
} OBJC2_UNAVAILABLE;
Person *person = [[Person alloc] init];
NSLog(@"person class:%p", [person class]); // 获取到类 objc_class结构体
NSLog(@"person meta class:%p", object_getClass(person)); // 获取到类 objc_class结构体
NSLog(@"person meta class:%p", [Person class]); // 获取到元类 objc_class结构体
NSLog(@"person meta class:%p", object_getClass([person class])); // 获取到元类 objc_class结构体