方法中包含sel与IMP,方法交换就是运行时将sel和imp原本的对应断开,并将sel和新的IMP生成对应关系。OC中通过方法交换可实现面向切面编程AOP。AOP更倾向于提取各模块中的公共部分,提高模块复用,降低业务耦合;OOP主要是对业务模块进行封装,划分清晰的逻辑。方法交换如下图(图片转载自月月_Style,感谢🙏)
Method-Swizzling实现
父类
@interface SLPerson : NSObject
-(void)personInstanceMethod;
@end
@implementation SLPerson
- (void)personInstanceMethod{
NSLog(@"person对象方法:%s",__func__);
}
@end
子类及其分类
@interface SLStudent : SLPerson
- (void)helloword;
@end
@implementation SLStudent
@end
@implementation SLStudent (SL)
+ (void)load {
NSLog(@"SLStudent (SL) load");
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
[Tool sl_methodSwizzlingWithClass:self oriSEL:@selector(helloword) swizzledSEL:@selector(sl_studentInstanceMethod)];
});
}
-(void)sl_studentInstanceMethod {
NSLog(@"LGStudent分类添加的lg对象方法:%s",__func__);
[self sl_studentInstanceMethod]; //lg_studentInstanceMethod -/-> personInstanceMethod
}
@end
由于load方法可能会多次调用,会导致方法重复交换,sel的指向可能会恢复成原imp,交换了个寂寞。这里使用dispatch_once_t设计单例,使得只执行一次方法交换。
Tool实现方法交换
@implementation Tool
+ (void)sl_methodSwizzlingWithClass:(Class)cls oriSEL:(SEL)oriSEL swizzledSEL:(SEL)swizzledSEL{
if (!cls) NSLog(@"传入的交换类不能为空");
Method oriMethod = class_getInstanceMethod(cls, oriSEL);
Method swiMethod = class_getInstanceMethod(cls, swizzledSEL);
if (!oriMethod) { // 避免动作没有意义
// 在oriMethod为nil时,替换后将swizzledSEL复制一个不做任何事的空实现,代码如下:
class_addMethod(cls, oriSEL, method_getImplementation(swiMethod), method_getTypeEncoding(swiMethod));
method_setImplementation(swiMethod, imp_implementationWithBlock(^(id self, SEL _cmd){
NSLog(@"来了一个空的 imp");
}));
}
// 一般交换方法: 交换自己有的方法 -- 走下面 因为自己有意味添加方法失败
// 交换自己没有实现的方法:
// 首先第一步:会先尝试给自己添加要交换的方法 :personInstanceMethod (SEL) -> swiMethod(IMP)
// 然后再将父类的IMP给swizzle personInstanceMethod(imp) -> swizzledSEL
//oriSEL:personInstanceMethod
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
方法交换时要避免imp找不到的情况,例如,如果父类的方法A被子类交换成子类自己独有的方法B时后,父类再调用方法A,会因为imp找不到而发生崩溃。同时,如果子类父类都没实现方法的情况下,方法交换会造成递归死循环,直接栈溢出崩溃。因此在oriMethod为nil时,给oriSEL
添加swiMethod
方法,然后替换后将swizzledSEL复制一个不做任何事的空实现。
method-swizzling的应用
可用来防止数组越界,埋点统计等。下面展示iOS中某些类簇的原形。
类名 | 原形 |
NSDictionary | __NSDictionaryI |
NSMutableDictionary | __NSDictionaryM |
NSArray | __NSArrayI |
NSMutableArray | __NSArrayM |