坑点1:多次进行方法交换,会将方法替换为原来的实现
解决方法:利用单利进行限制,只进行一次方法交换
// 解决坑点1
+ (void)load{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
[RuntimeTool wsk_methodSwizzlingWithClass:self oriSEL:@selector(wsk_oriFunction) swizzledSEL:@selector(wsk_swiMethodFunction)];
});
}
坑点2:交换的旧方法,子类未实现,父类实现
出现的问题:父类在调用旧方法时,会崩溃
解决方法:先进行方法添加,如果添加成功则进行替换<repleace
>,反之,则进行交换<exchange
>
坑点3:进行交换的方法,子类、父类均未实现
出现的问题:出现死循环
解决方法:如果旧方法为nil
,则替换后将swizzeldSEL
复制一个不做任何操作的空实现。
代码如下:
#import "RuntimeTool.h"
#import <objc/runtime.h>
@implementation RuntimeTool
+ (void)wsk_methodSwizzlingWithClass:(Class)cls oriSEL:(SEL)oriSEL swizzledSEL:(SEL)swizzledSEL{
if (!cls) NSLog(@"传入的交换类不能为空");
Method oriMethod = class_getInstanceMethod(cls, oriSEL);
Method swiMethod = class_getInstanceMethod(cls, swizzledSEL);
// 解决坑点3
if (!oriMethod) {
// 在oriMethod为nil时,替换后将swizzledSEL复制一个不做任何事的空实现,代码如下:
class_addMethod(cls, oriSEL, method_getImplementation(swiMethod), method_getTypeEncoding(swiMethod));
method_setImplementation(swiMethod, imp_implementationWithBlock(^(id self, SEL _cmd){ }));
}
// 一般交换方法: 交换自己有的方法 -- 走下面 因为自己有意味添加方法失败
// 交换自己没有实现的方法:
// 首先第一步:会先尝试给自己添加要交换的方法 :personInstanceMethod (SEL) -> swiMethod(IMP)
// 然后再将父类的IMP给swizzle personInstanceMethod(imp) -> swizzledSEL
//oriSEL:personInstanceMethod
// 解决坑点2
BOOL didAddMethod = class_addMethod(cls, oriSEL, method_getImplementation(swiMethod), method_getTypeEncoding(swiMethod));
if (didAddMethod) {
class_replaceMethod(cls, swizzledSEL, method_getImplementation(oriMethod), method_getTypeEncoding(oriMethod));
}else{
method_exchangeImplementations(oriMethod, swiMethod);
}
}
@end