- 发生场景及原因:
绝大多数情况下,我们向NSNull对象发送消息,都会产生崩溃,NSNull对象常见于后台返回数据中可能会有null字段,很多JSON库都会转成NSNull对象,如下情况就会产生崩溃:
id obj = [NSNull null];
NSLog(@"%@", [objstringValue]);
对此我们利用运行时来可以重写
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector
和
- (void)forwardInvocation:(NSInvocation *)anInvocation
这两个方法将没能力处理消息的方法签名转发给nil对象则不会产生崩溃
此外,常见的崩溃比如,NSArray取值越界,NSDictionary传了nil对象,这些问题产生的崩溃可以使用Runtime中的Method Swizzle,将原生的方法hook掉,如下:
#import "NSMutableDictionary+NullSafe.h"
#import <objc/runtime.h>
@implementation NSMutableDictionary (NullSafe)
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
id obj = [[self alloc] init];
[obj swizzleMethod:@selector(setObject:forKey:)withMethod:@selector(safe_setObject:forKey:)];
});
}
- (void)swizzleMethod:(SEL)origSelector withMethod:(SEL)newSelector
{
//
Class class = [self class];
/** 得到类的实例方法 class_getInstanceMethod(Class _Nullable __unsafe_unretained cls, SEL _Nonnull name)
_Nullable __unsafe_unretained cls 那个类
_Nonnull name 按个方法
补充: class_getClassMethod 得到类的 类方法
*/
// 必须两个Method都要拿到
Method originalMethod = class_getInstanceMethod(class, origSelector);
Method swizzledMethod = class_getInstanceMethod(class, newSelector);
/** 动态添加方法 class_addMethod(Class _Nullable __unsafe_unretained cls, SEL _Nonnull name, IMP _Nonnull imp, const char * _Nullable types)
class_addMethod 是相对于实现来的说的,将本来不存在于被操作的Class里的newMethod的实现添加在被操作的Class里,并使用origSel作为其选择子
_Nonnull name 原方法选择子,
_Nonnull imp 新方法选择子,
*/
// 如果发现方法已经存在,会失败返回,也可以用来做检查用,我们这里是为了避免源方法没有实现的情况;如果方法没有存在,我们则先尝试添加被替换的方法的实现
BOOL didAddMethod = class_addMethod(class,origSelector,method_getImplementation(swizzledMethod),method_getTypeEncoding(swizzledMethod));
// 如果返回成功:则说明被替换方法没有存在.也就是被替换的方法没有被实现,我们需要先把这个方法实现,然后再执行我们想要的效果,用我们自定义的方法去替换被替换的方法. 这里使用到的是class_replaceMethod这个方法. class_replaceMethod本身会尝试调用class_addMethod和method_setImplementation,所以直接调用class_replaceMethod就可以了)
if (didAddMethod) {
class_replaceMethod(class,newSelector,method_getImplementation(originalMethod),method_getTypeEncoding(originalMethod));
} else { // 如果返回失败:则说明被替换方法已经存在.直接将两个方法的实现交换即
method_exchangeImplementations(originalMethod, swizzledMethod);
}
}
- (void)safe_setObject:(id)value forKey:(NSString *)key {
if (value) {
[self safe_setObject:value forKey:key];
}else {
NSLog(@"[NSMutableDictionarysetObject: forKey:], Object cannot be nil");
}
}
@end
这种解决方法可以避免诸如数组取值越界、字典传空值、removeObjectAtIndex等错误,如下的崩溃就可以避免:
id obj = nil;
NSMutableDictionary *m_dict =[NSMutableDictionary dictionary];
[dict setObject:obj forKey:@"666"];