1、注意:进行方法交换时,交换双方方法的实现要确认都存在

  1. 如果缺少原方法的实现,则会交换失败陷入死循环,可以创建一个空实现的原方法,避免死循环
    1. 举例: 父类定义A方法,没有实现,子类定一个定义实现B方法,在子类中对AB方法进行交换。先对A的名字和B的实现进行捆绑,因为A方法没有实现,
  2. 如果缺少类中新的交换方法的实现,则会找不到方法而崩溃,需要先添加新的交换方法实现,如果添加成功,说明没有新的方法,再将旧的方法实现和新的方法名字进行绑定;如果失败已存在新的方法,直接进行交换即可
    1. 举例: 父类定义和实现A方法,子类定一个实现B方法,在子类中对AB方法进行交换。交换是成功的,调用子类的A方法,会执行到B方法。但是调用父类的A方法时,方法地址指向了B方法,但是父类中没有B方法,就会找不到方法而崩溃

对象方法的方法交换:

  1. + (void)lg_bestMethodSwizzlingWithClass:(Class)cls oriSEL:(SEL)oriSEL swizzledSEL:(SEL)swizzledSEL{
  2. if (!cls) NSLog(@"传入的交换类不能为空");
  3. Method oriMethod = class_getInstanceMethod(cls, oriSEL);
  4. Method swiMethod = class_getInstanceMethod(cls, swizzledSEL);
  5. // 没有原方法的实现时,添加
  6. if (!oriMethod) {
  7. // 在oriMethod为nil时,替换后将swizzledSEL复制一个不做任何事的空实现,代码如下:
  8. class_addMethod(cls, oriSEL, method_getImplementation(swiMethod), method_getTypeEncoding(swiMethod));
  9. method_setImplementation(swiMethod, imp_implementationWithBlock(^(id self, SEL _cmd){ }));
  10. }
  11. // 一般交换方法: 交换自己有的方法 -- 走下面 因为自己有意味添加方法失败
  12. // 交换自己没有实现的方法:
  13. // 首先第一步:会先尝试给自己添加要交换的方法 :personInstanceMethod (SEL) -> swiMethod(IMP)
  14. // 然后再将父类的IMP给swizzle personInstanceMethod(imp) -> swizzledSEL
  15. //oriSEL:personInstanceMethod
  16. BOOL didAddMethod = class_addMethod(cls, oriSEL, method_getImplementation(swiMethod), method_getTypeEncoding(swiMethod));
  17. if (didAddMethod) {
  18. class_replaceMethod(cls, swizzledSEL, method_getImplementation(oriMethod), method_getTypeEncoding(oriMethod));
  19. }else{
  20. method_exchangeImplementations(oriMethod, swiMethod);
  21. }
  22. }

类方法的方法交换:

  1. //封装的method-swizzling方法
  2. + (void)lg_bestClassMethodSwizzlingWithClass:(Class)cls oriSEL:(SEL)oriSEL swizzledSEL:(SEL)swizzledSEL{
  3. if (!cls) NSLog(@"传入的交换类不能为空");
  4. Method oriMethod = class_getClassMethod([cls class], oriSEL);
  5. Method swiMethod = class_getClassMethod([cls class], swizzledSEL);
  6. if (!oriMethod) { // 避免动作没有意义
  7. // 在oriMethod为nil时,替换后将swizzledSEL复制一个不做任何事的空实现,代码如下:
  8. class_addMethod(object_getClass(cls), oriSEL, method_getImplementation(swiMethod), method_getTypeEncoding(swiMethod));
  9. method_setImplementation(swiMethod, imp_implementationWithBlock(^(id self, SEL _cmd){
  10. NSLog(@"来了一个空的 imp");
  11. }));
  12. }
  13. // 一般交换方法: 交换自己有的方法 -- 走下面 因为自己有意味添加方法失败
  14. // 交换自己没有实现的方法:
  15. // 首先第一步:会先尝试给自己添加要交换的方法 :personInstanceMethod (SEL) -> swiMethod(IMP)
  16. // 然后再将父类的IMP给swizzle personInstanceMethod(imp) -> swizzledSEL
  17. //oriSEL:personInstanceMethod
  18. BOOL didAddMethod = class_addMethod(object_getClass(cls), oriSEL, method_getImplementation(swiMethod), method_getTypeEncoding(swiMethod));
  19. if (didAddMethod) {
  20. class_replaceMethod(object_getClass(cls), swizzledSEL, method_getImplementation(oriMethod), method_getTypeEncoding(oriMethod));
  21. }else{
  22. method_exchangeImplementations(oriMethod, swiMethod);
  23. }
  24. }

参考原文:
https://www.jianshu.com/p/f306adf3480d

2.方法交换用途:

  • 检测数组越界、数组里放nil , 防止崩溃 ```objectivec @implementation NSArray (CJLArray) //如果下面代码不起作用,造成这个问题的原因大多都是其调用了super load方法。在下面的load方法中,不应该调用父类的load方法。这样会导致方法交换无效
  • (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); }

//如果下面代码不起作用,造成这个问题的原因大多都是其调用了super load方法。在下面的load方法中,不应该调用父类的load方法。这样会导致方法交换无效

  • (id)cjl_objectAtIndex:(NSUInteger)index{ //判断下标是否越界,如果越界就进入异常拦截 if (self.count-1 < index) {

    1. // 这里做一下异常处理,不然都不知道出错了。

    ifdef DEBUG // 调试阶段

    1. return [self cjl_objectAtIndex:index];

    else // 发布阶段

    1. @try {
    2. return [self cjl_objectAtIndex:index];
    3. } @catch (NSException *exception) {
    4. // 在崩溃后会打印崩溃信息,方便我们调试。
    5. NSLog(@"---------- %s Crash Because Method %s ----------\n", class_getName(self.class), __func__);
    6. NSLog(@"%@", [exception callStackSymbols]);
    7. return nil;
    8. } @finally {
    9. }

    endif

    }else{ // 如果没有问题,则正常进行方法调用

    1. return [self cjl_objectAtIndex:index];

    } }

@end ```

  • 监控VC/APP生命周期、列表曝光/点击事件, 进行无埋点上报
  • 监控VC, 导航控制器生命周期, 检测内存泄漏(MLeakFinder)