+ (void)load {
//1、单例,避免多次交互
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Method oriMethod = class_getInstanceMethod(self, @selector(eat));
Method swiMethod = class_getInstanceMethod(self, @selector(swi_eat));
// 3、取得新方法swiMethod的实现和方法类型签名
//把新方法实现放到旧方法名中,此刻调用-eat就是调用-swi_eat
BOOL didAddMethod = class_addMethod(self, @selector(eat), method_getImplementation(swiMethod), method_getTypeEncoding(swiMethod));
if (didAddMethod) {
// 3.1、添加成功,当前类未实现,父类实现了-eat
// 此时不必做方法交换,直接将swiMethod的IMP替换为父类oriMethod的IMP即可
class_replaceMethod(self, @selector(swi_eat), method_getImplementation(oriMethod), method_getTypeEncoding(oriMethod));
}else{
// 3.2、正常情况,直接交换
method_exchangeImplementations(oriMethod, swiMethod);
}
});
}
iOS method swimming
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- iOS runtime(一)runtime之Property 详尽iOS runtime(二)runtime之Iv...
- 本文Demo传送门:MethodSwizzlingDemo 摘要:编程,只了解原理不行,必须实战才能知道应用场景。...
- 本文Demo传送门:MethodSwizzlingDemo 摘要:编程,只了解原理不行,必须实战才能知道应用场景。...
- 前言 在平时开发过程中我们好多时候都需要进行方法的交换,就会用到Method Swizzling方法,但是用的时候...
- Message Forwarding Sending a message to an object that do...