首先看下 Objective-C 的对象模型,每一个 Objective-C 对象都是一个指向 Class 的指针。Class 的结构以下:html
struct objc_class { Class _Nonnull isa OBJC_ISA_AVAILABILITY; #if !__OBJC2__ Class _Nullable super_class OBJC2_UNAVAILABLE; const char * _Nonnull name OBJC2_UNAVAILABLE; long version OBJC2_UNAVAILABLE; long info OBJC2_UNAVAILABLE; long instance_size OBJC2_UNAVAILABLE; struct objc_ivar_list * _Nullable ivars OBJC2_UNAVAILABLE; struct objc_method_list * _Nullable * _Nullable methodLists OBJC2_UNAVAILABLE; struct objc_cache * _Nonnull cache OBJC2_UNAVAILABLE; struct objc_protocol_list * _Nullable protocols OBJC2_UNAVAILABLE; #endif } OBJC2_UNAVAILABLE;
这个结构已经有不少的说明了,下面简单的再描述下objective-c
变量 Ivar 也是一个结构体,每一个 Class 中用变长结构体
的方式存储了 Class 的变量列表。 IVar 的定义以下,包含 名称、类型、偏移、占用空间。缓存
typedef struct objc_ivar *Ivar; struct objc_ivar { char * _Nullable ivar_name OBJC2_UNAVAILABLE; char * _Nullable ivar_type OBJC2_UNAVAILABLE; int ivar_offset OBJC2_UNAVAILABLE; #ifdef __LP64__ int space OBJC2_UNAVAILABLE; #endif } OBJC2_UNAVAILABLE;
这个变长结构体定义以下:函数
struct objc_ivar_list { int ivar_count OBJC2_UNAVAILABLE; #ifdef __LP64__ int space OBJC2_UNAVAILABLE; #endif /* variable length structure */ struct objc_ivar ivar_list[1] OBJC2_UNAVAILABLE; } OBJC2_UNAVAILABLE;
每一个方法 Method 的定义以下,包含 SEL 指向对外的命名,char * 型 的方法类型, IMP 方法指针,指向具体的函数实现。ui
typedef struct objc_method *Method; struct objc_method { SEL _Nonnull method_name OBJC2_UNAVAILABLE; char * _Nullable method_types OBJC2_UNAVAILABLE; IMP _Nonnull method_imp OBJC2_UNAVAILABLE; } OBJC2_UNAVAILABLE;
一样一个变长结构体来存储方法列表。Class 中的这个列表是个2级指针,因此能够向 Class 中动态的添加方法。spa
struct objc_method_list { struct objc_method_list * _Nullable obsolete OBJC2_UNAVAILABLE; int method_count OBJC2_UNAVAILABLE; #ifdef __LP64__ int space OBJC2_UNAVAILABLE; #endif /* variable length structure */ struct objc_method method_list[1] OBJC2_UNAVAILABLE; } OBJC2_UNAVAILABLE;
一样一个变长结构体存储以前找到的 Method。.net
1)、mask:能够认为是当前能达到的最大index(从0开始的),因此缓存的size(total)是mask+1;
2)、occupied:被占用的槽位,由于缓存是以散列表的形式存在的,因此会有空槽,而occupied表示当前被占用的数目。指针
他是经过 要查找的 Method 的 SEL 地址和 mask 作一系列运算来肯定 Method 的存储与查找位置。更详细的说明能够看参考4。其中提到的几点也在说下:
子类的 cache 会存储在父类中找到的方法;cache 的大小会动态增长,可是增长以前必定会先清空本身(变长结构体的特性)。code
typedef struct objc_cache *Cache OBJC2_UNAVAILABLE; #define CACHE_BUCKET_NAME(B) ((B)->method_name) #define CACHE_BUCKET_IMP(B) ((B)->method_imp) #define CACHE_BUCKET_VALID(B) (B) #ifndef __LP64__ #define CACHE_HASH(sel, mask) (((uintptr_t)(sel)>>2) & (mask)) #else #define CACHE_HASH(sel, mask) (((unsigned int)((uintptr_t)(sel)>>3)) & (mask)) #endif struct objc_cache { unsigned int mask /* total = mask + 1 */ OBJC2_UNAVAILABLE; unsigned int occupied OBJC2_UNAVAILABLE; Method _Nullable buckets[1] OBJC2_UNAVAILABLE; };
typedef struct objc_category *Category; struct objc_category { char * _Nonnull category_name OBJC2_UNAVAILABLE; char * _Nonnull class_name OBJC2_UNAVAILABLE; struct objc_method_list * _Nullable instance_methods OBJC2_UNAVAILABLE; struct objc_method_list * _Nullable class_methods OBJC2_UNAVAILABLE; struct objc_protocol_list * _Nullable protocols OBJC2_UNAVAILABLE; } OBJC2_UNAVAILABLE; struct objc_protocol_list { struct objc_protocol_list * _Nullable next; long count; __unsafe_unretained Protocol * _Nullable list[1]; };
看一张经典的图:htm
isa 代表当前对象所属于的 Class 类型(Class 也是一个对象,Class 的类型叫 MetaClass)。
superClass 代表当前对象从哪一个父类派生出来的,根类型(好比 NSObject、NSProxy)的 superClass 是 nil。
向对象发送消息时,会去方法列表里面查询,找不到会去父类的方法列表,再找不到会进入动态添加、消息转发、消息包装的过程。向 Class 发送消息时,会去 MetaClass 走一样的过程。
self 是类的隐藏的参数,指向当前调用方法的类
super 是一个"编译器指示符", 是一个标记,告诉编译器起始于当前类的父类方法列表中搜索方法的实现。
看一个例子
@A - (void)show{ } - (void)log { NSLog(@"i am a"); } - (void)print { NSLog(@"i am %@",[self class]); } @end @B: A - (void)show { [self/super log]; [self/super print]; } - (void)log { NSLog(@"i am b"); } - (void)print { NSLog(@"i am %@",[self class]); } @end @ C: B - (void)log { NSLog(@"i am c"); } @end
在 B 的show 方法中分别改为 self 和 super,以下调用会输出什么?
C *c = [[C alloc] init]; [c show];
结果是 self 的时候 输出
i am c i am C
super 的时候输出
i am a i am C
用 self 调用方法,会编译成 objc_msgSend
方法,其定义以下:
void objc_msgSend(void /* id self, SEL op, ... */ )
第一个参数是消息接收者,也就是对象自己,第二个参数是调用的具体类方法的 selector。这里有个隐藏参数 _cmd
,表明当前类方法的selector。
用super 调用方法,会编译成 objc_msgSendSuper
方法,其定义以下:
void objc_msgSendSuper(void /* struct objc_super *super, SEL op, ... */ )
其中 objc_super
的定义以下:
/// Specifies the superclass of an instance. struct objc_super { /// Specifies an instance of a class. __unsafe_unretained _Nonnull id receiver; /// Specifies the particular superclass of the instance to message. #if !defined(__cplusplus) && !__OBJC2__ /* For compatibility with old objc-runtime.h header */ __unsafe_unretained _Nonnull Class class; #else __unsafe_unretained _Nonnull Class super_class; #endif /* super_class is the first class to search */ };
当向一个类的实例发送方法时,会去 Class
结构的方法缓存列表 objc_cache
和 方法列表 objc_method_list
中查找有没有这个方法,若是没有的话,则会进入消息转发阶段。
消息转发主要分为两大阶段:
动态方法解析:看对象所属类是否能动态添加方法
转发阶段:既然第一步已经不会新增方法来响应,那系统就会请接受者看看有没有其余对象响应这个消息;若是没有,就把消息封装到 NSInvocation中,再作一次尝试。
参考:
1.http://www.sealiesoftware.com/blog/archive/2013/09/24/objc_explain_Non-pointer_isa.html
2.http://time-track.cn/variable-length-structure.html
3.https://tech.meituan.com/DiveIntoMethodCache.html
4.http://blog.csdn.net/datacloud/article/details/7275170
5.http://blog.csdn.net/wzzvictory/article/details/8487111