1、注意:进行方法交换时,交换双方方法的实现要确认都存在
- 如果缺少原方法的实现,则会交换失败陷入死循环,可以创建一个空实现的原方法,避免死循环
- 举例: 父类定义A方法,没有实现,子类定一个定义实现B方法,在子类中对AB方法进行交换。先对A的名字和B的实现进行捆绑,因为A方法没有实现,
- 如果缺少类中新的交换方法的实现,则会找不到方法而崩溃,需要先添加新的交换方法实现,如果添加成功,说明没有新的方法,再将旧的方法实现和新的方法名字进行绑定;如果失败已存在新的方法,直接进行交换即可
- 举例: 父类定义和实现A方法,子类定一个实现B方法,在子类中对AB方法进行交换。交换是成功的,调用子类的A方法,会执行到B方法。但是调用父类的A方法时,方法地址指向了B方法,但是父类中没有B方法,就会找不到方法而崩溃
对象方法的方法交换:
+ (void)lg_bestMethodSwizzlingWithClass:(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){ }));}// 一般交换方法: 交换自己有的方法 -- 走下面 因为自己有意味添加方法失败// 交换自己没有实现的方法:// 首先第一步:会先尝试给自己添加要交换的方法 :personInstanceMethod (SEL) -> swiMethod(IMP)// 然后再将父类的IMP给swizzle personInstanceMethod(imp) -> swizzledSEL//oriSEL:personInstanceMethodBOOL 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);}}
类方法的方法交换:
//封装的method-swizzling方法+ (void)lg_bestClassMethodSwizzlingWithClass:(Class)cls oriSEL:(SEL)oriSEL swizzledSEL:(SEL)swizzledSEL{if (!cls) NSLog(@"传入的交换类不能为空");Method oriMethod = class_getClassMethod([cls class], oriSEL);Method swiMethod = class_getClassMethod([cls class], swizzledSEL);if (!oriMethod) { // 避免动作没有意义// 在oriMethod为nil时,替换后将swizzledSEL复制一个不做任何事的空实现,代码如下:class_addMethod(object_getClass(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:personInstanceMethodBOOL didAddMethod = class_addMethod(object_getClass(cls), oriSEL, method_getImplementation(swiMethod), method_getTypeEncoding(swiMethod));if (didAddMethod) {class_replaceMethod(object_getClass(cls), swizzledSEL, method_getImplementation(oriMethod), method_getTypeEncoding(oriMethod));}else{method_exchangeImplementations(oriMethod, swiMethod);}}
参考原文:
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) {
// 这里做一下异常处理,不然都不知道出错了。
ifdef DEBUG // 调试阶段
return [self cjl_objectAtIndex:index];
else // 发布阶段
@try {return [self cjl_objectAtIndex:index];} @catch (NSException *exception) {// 在崩溃后会打印崩溃信息,方便我们调试。NSLog(@"---------- %s Crash Because Method %s ----------\n", class_getName(self.class), __func__);NSLog(@"%@", [exception callStackSymbols]);return nil;} @finally {}
endif
}else{ // 如果没有问题,则正常进行方法调用
return [self cjl_objectAtIndex:index];
} }
@end ```
- 监控VC/APP生命周期、列表曝光/点击事件, 进行无埋点上报
- 监控VC, 导航控制器生命周期, 检测内存泄漏(MLeakFinder)
