// 2019開工第一天座慰,今年想好好學(xué)習(xí);看了下Runtime鹃祖,然后簡單記錄下钓猬,怕自己忘記了:
// 添加方法
BOOL class_addMethod ( Class cls, SEL name, IMP imp, const char *types );
// 獲取實(shí)例方法
Method class_getInstanceMethod ( Class cls, SEL name );
// 獲取類方法
Method class_getClassMethod ( Class cls, SEL name );
// 獲取所有方法的List
Method * class_copyMethodList ( Class cls, unsigned int *outCount );
// 替代方法的實(shí)現(xiàn)
IMP class_replaceMethod ( Class cls, SEL name, IMP imp, const char *types );
// 返回方法的具體實(shí)現(xiàn)
IMP class_getMethodImplementation ( Class cls, SEL name );
IMP class_getMethodImplementation_stret ( Class cls, SEL name );
// 類實(shí)例是否響應(yīng)指定的selector
BOOL class_respondsToSelector ( Class cls, SEL sel );
??:當(dāng)判斷一個實(shí)例方法是否實(shí)現(xiàn)時,第一個參數(shù)要用類對象匀借,也就是[Person class]颜阐。
當(dāng)判斷一個類方法是否實(shí)現(xiàn)時,第一個參數(shù)要傳元類吓肋,也就是object_getClass([Person class])凳怨。
objc_msgSend 動態(tài)添加方法
申明一個ViewController類和Person類
在person類中實(shí)現(xiàn)方法
/**
無參數(shù)無返回值
*/
-(void)fun
{
NSLog(@"fun");
}
/**
有參數(shù)無返回值
@param food food
@param some some
*/
-(void)eat:(NSString *)food say:(NSString *)some
{
NSLog(@"%@ %@",food, some);
}
/**
有參數(shù)有返回值
@param value value
@return NSString
*/
- (NSString *)getString:(NSString *)value{
NSLog(@"ba\\value = %@",value);
return value;
}
然后在VC中對應(yīng)實(shí)現(xiàn)相應(yīng)的方法:
// 無參數(shù)無返回值
((void (*)(id, SEL))objc_msgSend)(p, @selector(fun)); //正確寫法
// 有參數(shù)無返回值
void (*glt_msgsend)(id, SEL, NSString *, NSString *) = (void (*)(id, SEL, NSString *, NSString *))objc_msgSend;
glt_msgsend(p, @selector(eat:say:), @"123", @"456");
// 有參數(shù)有返回值
NSString* (*String_msgsend)(id, SEL, NSString *) = (NSString * (*)(id, SEL, NSString *))objc_msgSend;
String_msgsend(p, @selector(getString:), @"value");
class_addMethod 使用
/*
1.cls 哪個類
2.SEL
3.IMP
4.返回值類型!
*/
class_addMethod ( Class cls, SEL name, IMP imp, const char *types );
如果知道對應(yīng)的函數(shù)返回值和類型,則可以直接使用
// i:返回值類型int是鬼,若是v則表示void
class_addMethod(self, sel, (IMP)haha, "v@:@");
void haha(id obj,SEL sel,NSString * objc){
NSLog(@"%@--%@--%@",obj,NSStringFromSelector(sel),objc);
}
否則可以用下面的方法:
SEL swizzledMethod = @selector(systemMethod_PrintLog);
Method swizzledMethod = class_getInstanceMethod(class, originalSelector);
class_addMethod(class,
originalSelector,
method_getImplementation(swizzledMethod),
method_getTypeEncoding(swizzledMethod));
附加:方法的替換
load是只要類所在文件被引用就會被調(diào)用肤舞,而initialize是在類或者其子類的第一個方法被調(diào)用前調(diào)用。所以如果類沒有被引用進(jìn)項(xiàng)目均蜜,就不會有l(wèi)oad調(diào)用李剖;但即使類文件被引用進(jìn)來,但是沒有使用囤耳,那么initialize也不會被調(diào)用篙顺。load的調(diào)用會引起initialize的調(diào)用。
/**
消息替換方法
*/
+ (void)load {
NSLog(@"load");
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Class class = [self class];
SEL originalSelector = @selector(systemMethod_PrintLog);
SEL swizzledSelector = @selector(ll_imageName);
Method originalMethod = class_getInstanceMethod(class, originalSelector);
Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
BOOL didAddMethod =
class_addMethod(class,
originalSelector,
method_getImplementation(swizzledMethod),
method_getTypeEncoding(swizzledMethod));
if (didAddMethod) {
class_replaceMethod(class,
swizzledSelector,
method_getImplementation(originalMethod),
method_getTypeEncoding(originalMethod));
} else {
method_exchangeImplementations(originalMethod, swizzledMethod);
}
});
}
+(void)initialize{
NSLog(@"initialize");
}
當(dāng)一個methad沒實(shí)現(xiàn)可能會引起Crash時候充择,方法的攔截
//是為類方法進(jìn)行決議德玫。class_addMethod添加Method
+ (BOOL)resolveClassMethod:(SEL)name;
// 是為對象方法進(jìn)行決議,
+ (BOOL)resolveInstanceMethod:(SEL)name;
eg1:
//如果該類接收到一個沒有實(shí)現(xiàn)的實(shí)例方法,就會來到這里
+(BOOL)resolveInstanceMethod:(SEL)sel
{
// NSLog(@"%@",NSStringFromSelector(sel));
//動態(tài)添加一個方法!!
/*
1.cls 哪個類
2.SEL
3.IMP
4.返回值類型!
*/
class_addMethod(self, sel, (IMP)haha, "v@:@");
return [super resolveInstanceMethod:sel];
}
void haha(id obj,SEL sel,NSString * objc){
NSLog(@"%@--%@--%@",obj,NSStringFromSelector(sel),objc);
}
eg2:
- (void) run {
NSLog(@"%s", __func__);
}
+ (BOOL)resolveInstanceMethod:(SEL)sel {
NSLog(@"%s", __func__);
// // 元類
// // 實(shí)例對象椎麦、類對象宰僧、元類對象
// if (sel == @selector(walk)) {
// return class_addMethod(self, sel, (IMP)walk, "v@:");
// }
if (sel == @selector(walk)) {
Method runMethod = class_getInstanceMethod(self, @selector(run));
IMP runIMP = method_getImplementation(runMethod);
const char* types = method_getTypeEncoding(runMethod);
NSLog(@"%s", types);
return class_addMethod(self, sel, runIMP, types);
}
return [super resolveInstanceMethod:sel];
}
eg3:
+ (void) run {
NSLog(@"%s", __func__);
}
+ (BOOL)resolveClassMethod:(SEL)sel {
if (sel == @selector(walk)) {
Method runMethod = class_getClassMethod(self, @selector(run));
IMP runIMP = method_getImplementation(runMethod);
const char* types = method_getTypeEncoding(runMethod);
NSLog(@"%s", types);
return class_addMethod(object_getClass(self), sel, runIMP, types);
}
return [super resolveClassMethod:sel];
}
參考鏈接:
Runtime基礎(chǔ)使用場景-攔截替換方法
class_addMethod以及消息轉(zhuǎn)發(fā)機(jī)制
objc_msgSend的使用注意事項(xiàng)