ISA
@interface Object
{
Class isa; /* A pointer to the instance's class structure */
}
每一個(gè)類都會(huì)有isa指針歧譬,該指針指向類的結(jié)構(gòu)體岸浑,如在底層objc_msgSent() 就是通過isa來(lái)查找該類的。再通過該類的super class查找父類等
SEL
只是代表了選擇器(方法名) 并不是方法的實(shí)現(xiàn)瑰步。
每一類都有一個(gè)調(diào)度表(dispatch table) 該表存儲(chǔ)SEL 和Imp SEL相當(dāng)于方法的名 并沒有實(shí)現(xiàn) 當(dāng)調(diào)用函數(shù)是可以根據(jù)sel 找到Imp imp則為指向函數(shù)實(shí)現(xiàn)
SEL的獲取與使用
//第一種獲取方法
SEL ABC = @selector(aaa:);
//第二種獲取方法(建議使用這個(gè))
SEL ABC1 = NSSelectorFromString(@"aaa:");
[self performSelector:ABC];
IMP
函數(shù)現(xiàn)實(shí)指針
#if !OBJC_OLD_DISPATCH_PROTOTYPES
typedef void (*IMP)(void /* id, SEL, ... */ );
#else
typedef id (*IMP)(id, SEL, ...);
#endif
我們可以自己構(gòu)建IMP指針
/**
動(dòng)態(tài)添加方法(這個(gè)是定義了一個(gè)函數(shù)實(shí)現(xiàn)指針I(yè)MP)
@param self 接收者
@param _cmd 選擇器
@param value 參數(shù)
*/
void dynamicMethod(id self, SEL _cmd, id value) {
NSLog(@"首先嘗試動(dòng)態(tài)添加方法");
}
IMP imp = (IMP)dynamicMethod;
//也可以通過選擇器獲取其IMP
IMP methodPoint = [self methodForSelector:methodId];
//通過runtime獲取IMP
IMP imp = class_getMethodImplementation([self class], @selector(abc:));