方法交换
对于runtime
机制,SEL
和IMP
结构体成员方式存储在Method
中
struct objc_method {
SEL _Nonnull method_name OBJC2_UNAVAILABLE;
char * _Nullable method_types OBJC2_UNAVAILABLE;
IMP _Nonnull method_imp OBJC2_UNAVAILABLE;
} OBJC2_UNAVAILABLE;
对于一个Method
,一个SEL
对应一个IMP
,objc_cache机制
也是通过SEL
来查找,通过method->method_name == sel
判断,这里贴一段缓存查找的汇编语言解释
macro CacheLookup /* selReg, classReg, missLabel */
MOVE r9, $0, LSR #2 /* index = (sel >> 2) */
ldr a4, [$1, #CACHE] /* cache = class->cache */
add a4, a4, #BUCKETS /* buckets = &cache->buckets */
/* search the cache */
/* a1=receiver, a2 or a3=sel, r9=index, a4=buckets, $1=method */
1:
ldr ip, [a4, #NEGMASK] /* mask = cache->mask */
and r9, r9, ip /* index &= mask */
ldr $1, [a4, r9, LSL #2] /* method = buckets[index] */
teq $1, #0