- 發(fā)生場景及原因:
絕大多數情況下,我們向NSNull對象發(fā)送消息,都會產生崩潰,NSNull對象常見于后臺返回數據中可能會有null字段,很多JSON庫都會轉成NSNull對象,如下情況就會產生崩潰:
id obj = [NSNull null];
NSLog(@"%@", [objstringValue]);
對此我們利用運行時來可以重寫
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector和
- (void)forwardInvocation:(NSInvocation *)anInvocation這兩個方法將沒能力處理消息的方法簽名轉發(fā)給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);
/** 動態(tài)添加方法 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 新方法選擇子,
*/
// 如果發(fā)現方法已經存在,會失敗返回,也可以用來做檢查用,我們這里是為了避免源方法沒有實現的情況;如果方法沒有存在,我們則先嘗試添加被替換的方法的實現
BOOL didAddMethod = class_addMethod(class,origSelector,method_getImplementation(swizzledMethod),method_getTypeEncoding(swizzledMethod));
// 如果返回成功:則說明被替換方法沒有存在.也就是被替換的方法沒有被實現,我們需要先把這個方法實現,然后再執(zhí)行我們想要的效果,用我們自定義的方法去替換被替換的方法. 這里使用到的是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"];