1. Method-Swizzling
1.1 简介
-
Runtime中的黑魔法,运行时替换方法的实现 -
OC中利用Method-Swizzling实现AOP(面向切片编程) - 每个方法
Method中都有SEL和IMP,方法交换,就是将SEL和IMP的对应关系断开,将SEL和新的IMP建立关系
如下图所示:
image.jpeg
1.2 相关的 API
// 通过 sel 获取实例方法
class_getInstanceMethod
// 通过 sel 获取类方法
class_getClassMethod
// 获取一个方法的实现
method_getImplementation
// 设置一个方法的实现
method_setImplementation
// 获取方法实现的编码类型
method_getTypeEncoding
// 添加方法实现
class_addMethod
// 用一个方法的实现,替换另一个方法的实现,并不是交换
class_replaceMethod:
// 交换两个方法的实现
method_exchangeImplementations
2. 问题记录
2.1 method-swizzling使用过程中的一次性问题
如果写在+load 方法中会调用多次,这样会导致方法的重复交换,使 sel 的指向又恢复成原来的imp,可以使用 dispatch_once 实现只调用一次:
+ (void)load{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
[LGRuntimeTool lg_bestMethodSwizzlingWithClass:self oriSEL:@selector(helloword) swizzledSEL:@selector(lg_studentInstanceMethod)];
});
}
2.2 子类中替换从父类继承的方法
在下面这段代码中,Person 实现了personInstanceMethod,而 Teahcer 继承自Person,但没有实现personInstanceMethod,并且将personInstanceMethod替换成了自己的方法实现,看下面这段代码会有什么问题?
/////////////////////// Person类:
@interface Person : NSObject
- (void)personInstanceMethod;
@end
@implementation Person
- (void)personInstanceMethod{
NSLog(@"person 对象方法:%s",__func__);
}
@end
//////////////////////// Teacher类
@interface Teacher : Person
@end
@implementation Teacher
+ (void)load{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
// 交换方法
Method oriMethod = class_getInstanceMethod(self, @selector(personInstanceMethod));
Method swiMethod = class_getInstanceMethod(self, @selector(lg_teacherInstanceMethod));
method_exchangeImplementations(oriMethod, swiMethod);
});
}
// 这里是一个注意的点,这里并不是递归调用,因为已经交换完毕了,lg_teacherInstanceMethod会调用到 oriIMP,即 personInstanceMethod 的方法实现
- (void) lg_teacherInstanceMethod{
[self lg_teacherInstanceMethod];
NSLog(@"Teahcer分类添加的lg对象方法:%s",__func__);
}
@end
////////////// 调用:
- (void)viewDidLoad {
[super viewDidLoad];
Person *person = [Person alloc];
[person personInstanceMethod];
Teacher *teacher = [Teacher alloc];
[teacher personInstanceMethod];
}
直接运行代码,会发现 person 调用personInstanceMethod方法时产生崩溃:
截屏2021-01-15 上午11.00.32.png
personInstanceMethod的方法实现在 Teacher类中被替换成了lg_teacherInstanceMethod的方法实现,但是这个方法实现是写在 Teacher类中的,在 Person类中并没有这个方法实现,所以当调用时找不到相关的imp,产生崩溃。
我们这样替换了父类的方法,影响到了父类,所以正确的做法是先将oriSEL 方法尝试添加到 Teacher 类中,如果Teacher类有这个方法(不是从父类继承的),就不添加,直接exchange,否则给Teacher类添加oriSEL方法,再将swiSEL的实现设置成oriSEL的实现,这样不会影响其父类。
这里将替换方法抽取出来,如下:
+ (void)methodSwizzlingWithClass:(Class)cls oriSEL:(SEL)oriSEL swizzledSEL:(SEL)swizzledSEL{
if (!cls) NSLog(@"传入的交换类不能为空");
Method oriMethod = class_getInstanceMethod(cls, oriSEL);
Method swiMethod = class_getInstanceMethod(cls, swizzledSEL);
// class_addMethod方法内部会判断当前类是否有 oriSEL 方法(不是从父类继承的),如果没有则会添加 oriSEL 方法 ,方法实现为swiMethod
BOOL success = class_addMethod(cls, oriSEL, method_getImplementation(swiMethod), method_getTypeEncoding(swiMethod));
if (success) {// 如果没有,添加成功后就进行方法替换, 将 swiSEL 方法的实现替换成 oriSEL 方法的实现
class_replaceMethod(cls, swizzledSEL, method_getImplementation(oriMethod), method_getTypeEncoding(oriMethod));
}else{ // 添加失败,说明当前类有这个方法,直接交换
method_exchangeImplementations(oriMethod, swiMethod);
}
}
- 下面是
class_replaceMethod、class_addMethod和method_exchangeImplementations的源码实现:
image.jpeg
- 其中
class_replaceMethod和class_addMethod中都调用了addMethod方法,区别在于最后replace值的判断,下面是addMethod的实现:
image.jpeg
注意:
getMethodNoSuper_nolock是判断自己类是否有这个方法,不会从父类中查找判断。
2.3 子类中没有实现,父类也没有实现
在调用personInstanceMethod方法时,父类Person中只有声明,没有实现,子类Teacher中既没有声明,也没有实现,
/////////////////////// Person类:
@interface Person : NSObject
- (void)personInstanceMethod;
@end
@implementation Person
@end
//////////////////////// Teacher类
@interface Teacher : Person
@end
@implementation Teacher
@end
经过调试,发现运行代码会崩溃,报错结果如下所示:
截屏2021-01-16 下午7.13.33.png
原因是 personInstanceMethod没有实现,当给 lg_teacherInstanceMethod设置oriMethod 的方法实现时,由于 oriMethod 的 imp 为空,所以设置失败,导致lg_teacherInstanceMethod会一直调用自己:
截屏2021-01-16 下午7.31.09.png
如果oriMethod 为空,为了崩溃需要额外加一层判断:
- 通过
class_addMethod给oriSEL添加swiMethod方法实现 - 通过
method_setImplementation将swiMethod的imp指向不做任何事的空实现
+ (void)exchangeImpWithClass:(Class)class oriSEL:(SEL)oriSEL swiSEL:(SEL)swiSEL{
Method oriMethod = class_getInstanceMethod(class, oriSEL);
Method swiMethod = class_getInstanceMethod(class, swiSEL);
if(!oriMethod){
class_addMethod(class, oriSEL, method_getImplementation(swiMethod), method_getTypeEncoding(oriMethod));
method_setImplementation(swiMethod, imp_implementationWithBlock(^(id self, SEL _cmd){
NSLog(@"来了一个空的 imp");
}));
}
BOOL success = class_addMethod(class, oriSEL, method_getImplementation(swiMethod), method_getTypeEncoding(swiMethod));
if(success){
class_replaceMethod(class, swiSEL, method_getImplementation(oriMethod), method_getTypeEncoding(oriMethod));
}else{
method_exchangeImplementations(oriMethod, swiMethod);
}
}
3. 使用场景
一般用的挺多的是防止数组、字典等越界崩溃,或者添加的value 值为 nil,有一个注意的点:在iOS中NSNumber、NSArray、NSDictionary等这些类都是类簇,一个NSArray的实现可能由多个类组成。所以如果想对NSArray进行Swizzling,必须获取到其“真身”进行Swizzling,直接对NSArray进行操作是无效的。
下面列举了NSArray和NSDictionary本类的类名,可以通过Runtime函数取出本类。
| 类 | 真身 |
|---|---|
| NSArray | __NSArrayI |
| NSMutableArray | __NSArrayM |
| NSDictionary | __NSDictionaryI |
| NSMutableDictionary | __NSDictionaryM |
比如替换NSArray 的方法,需要获取类名为__NSArrayI的类:
@implementation NSArray (Safe)
+ (void)load{
Method fromMethod = class_getInstanceMethod(objc_getClass("__NSArrayI"), @selector(objectAtIndex:));
Method toMethod = class_getInstanceMethod(objc_getClass("__NSArrayI"), @selector(cjl_objectAtIndex:));
method_exchangeImplementations(fromMethod, toMethod);
}
@end















网友评论