在OC中类, 对象和方法, 都是c结构体
struct objc_class { Class _Nonnull isa; //isa Class类型的指针 #if !__OBJC2__ Class _Nullable super_class; const char * _Nonnull name; long version; long info; long instance_size ; struct objc_ivar_list * _Nullable ivars ; struct objc_method_list * _Nullable * _Nullable methodLists ; struct objc_cache * _Nonnull cache ; struct objc_protocol_list * _Nullable protocols ; #endif } ; /* Use `Class` instead of `struct objc_class *` */typedef struct objc_class *Class;
struct objc_object { Class _Nonnull isa OBJC_ISA_AVAILABILITY; };typedef struct objc_object *id;
Class是一个objc_class结构类型的指针, id是一个objc_object结构类型的指针
isa: Class类型的指针, 实例对象有个isa, 指向Class, Class也有isa, 指向meteClasssuper_class 指向该类的父类, 如果该类已经是最顶层的根类(NSObject), 那么super_class就是NULLname 类的名字version 类的版本信息info 提供运行时期使用的一些位标识 1字节 = 8位, 节约内存instance_size 该类的实例变量的大小ivars 成员变量的链表 struct objc_ivar_list { int ivar_count ; #ifdef __LP64__ int space ; #endif /* variable length structure */ struct objc_ivar ivar_list[1] ; } ;objc_ivar
struct objc_ivar { char * _Nullable ivar_name; char * _Nullable ivar_type; int ivar_offset; #ifdef __LP64__ int space; #endif } methodLists 方法定义的链表 struct objc_method_list { struct objc_method_list * _Nullable obsolete; int method_count; #ifdef __LP64__ int space; #endif /* variable length structure */ struct objc_method method_list[1]; }objc_method_list 本质是一个有objc_method元素的可变长度的链表
objc_method
struct objc_method { SEL _Nonnull method_name; char * _Nullable method_types; IMP _Nonnull method_imp; }cache 优化机制, 指向最近使用的方法, 用于方法调用的优化
protocols 协议链表
struct objc_protocol_list { struct objc_protocol_list * _Nullable next; long count; __unsafe_unretained Protocol * _Nullable list[1]; };