最近在整理自己的技能树和学习随笔,准备作为自己的学习笔记
Rumtime 相关
1、最近碰到的诡异问题,在IOS10以下的APPStore Release包出现的莫名其妙的[NSBundle initWithURL ]崩溃,完全没啥头绪,又赶着发布版本,所有请出神器
```
#import "NSBundle+SafeBundle.h"#import#import@implementation NSBundle (SafeBundle)
+ (void)load {
//方法交换应该被保证,在程序中只会执行一次
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
//获得viewController的生命周期方法的selector
SEL systemSel = @selector(initWithURL:);
//自己实现的将要被交换的方法的selector
SEL swizzSel = @selector(safe_initWithURL:);
//两个方法的Method
Method systemMethod = class_getInstanceMethod([self class], systemSel);
Method swizzMethod = class_getInstanceMethod([self class], swizzSel);
//首先动态添加方法,实现是被交换的方法,返回值表示添加成功还是失败
BOOL isAdd = class_addMethod(self, systemSel, method_getImplementation(swizzMethod), method_getTypeEncoding(swizzMethod));
if (isAdd) {
//如果成功,说明类中不存在这个方法的实现
//将被交换方法的实现替换到这个并不存在的实现
class_replaceMethod(self, swizzSel, method_getImplementation(systemMethod), method_getTypeEncoding(systemMethod));
} else {
//否则,交换两个方法的实现
method_exchangeImplementations(systemMethod, swizzMethod);
}
});
}
- (instancetype)safe_initWithURL:(NSURL *)url {
// if ([[UIDevice currentDevice] systemVersion].floatValue < 10.0 ) {
if (!url || ![url isKindOfClass:[NSURL class]]) {
NSLog(@"无效的url");
return [NSBundle mainBundle];
}
// }
return [self safe_initWithURL:url];
}
```