Method-Swizzling 方法交换

方法中包含selIMP,方法交换就是运行时将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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值