由于之前做項目的時候,時不時會有無法預(yù)見的錯誤谈为,導(dǎo)致App上線了旅挤,Crash的時候,用戶體驗非常不好伞鲫。查過各方面的資料后粘茄,決定把這些無法預(yù)見的Crash給解決的,至少先保證App能正常用秕脓。
想了下柒瓣,其實也不難,就是對NSString吠架、NSMutableString芙贫、NSArray、NSMutableArray傍药、NSDictionary磺平、NSMutableDictionary、KVO 這些常用的類方法做了方法替換拐辽,數(shù)組索引越界褪秀、字典數(shù)據(jù)對象為nil的異常處理
@implementation NSArray (GJNoCrash)
void array_method_exchangeImplementations(Class cls, SEL name, SEL name2) {
Method fromMethod = class_getInstanceMethod(cls, name);
Method toMethod = class_getInstanceMethod(cls, name2);
method_exchangeImplementations(fromMethod, toMethod);
}
+ (void)load
{
Class __NSArray = objc_getClass("NSArray");
Class __NSArrayI = objc_getClass("__NSArrayI");
Class __NSArray0 = objc_getClass("__NSArray0");
Class __NSSingleObjectArrayI = objc_getClass("__NSSingleObjectArrayI");
array_method_exchangeImplementations(__NSArray, @selector(arrayWithObjects:count:), @selector(safeArrayWithObjects:count:));
array_method_exchangeImplementations(__NSArray, @selector(objectsAtIndexes:), @selector(safeObjectsAtIndexes:));
array_method_exchangeImplementations(__NSArrayI, @selector(objectAtIndexedSubscript:), @selector(safeObjectAtIndexedSubscript:));
array_method_exchangeImplementations(__NSArray0, @selector(objectAtIndex:), @selector(safeObjectAtIndexedNullarray:));
array_method_exchangeImplementations(__NSSingleObjectArrayI, @selector(objectAtIndex:), @selector(safeObjectAtIndexedArrayCountOnlyOne:));
array_method_exchangeImplementations(__NSArray, @selector(getObjects:range:), @selector(safeGetObjectsArray:range:));
array_method_exchangeImplementations(__NSArrayI, @selector(getObjects:range:), @selector(safeGetObjectsArrayI:range:));
array_method_exchangeImplementations(__NSSingleObjectArrayI, @selector(getObjects:range:), @selector(safeGetObjectsNSSingleObjectArrayI:range:));
}
- (instancetype)safeArrayWithObjects:(const id _Nonnull __unsafe_unretained *)objects count:(NSUInteger)cnt
{
id instance = nil;
@try {
instance = [self safeArrayWithObjects:objects count:cnt];
}
@catch (NSException *exception) {
//以下是對錯誤數(shù)據(jù)的處理,把為nil的數(shù)據(jù)去掉,然后初始化數(shù)組
NSInteger newObjsIndex = 0;
id _Nonnull __unsafe_unretained newObjects[cnt];
for (int i = 0; i < cnt; i++) {
if (objects[i] != nil) {
newObjects[newObjsIndex] = objects[i];
newObjsIndex++;
}
}
instance = [self safeArrayWithObjects:newObjects count:newObjsIndex];
}
@finally {
return instance;
}
}
@end
大概原理就是這樣
常見異常
NSString *s = @"aa";
NSString *ss = [s substringFromIndex:10];
NSLog(@"ss:%@", ss);
NSMutableString *s = [NSMutableString stringWithFormat:@"aaaa"];
NSString *ss = [s stringByReplacingCharactersInRange:NSMakeRange(10, 10) withString:@"bb"];
NSLog(@"ss:%@", ss);
NSArray *a = [NSArray array];
id v = [a objectAtIndex:10];
NSLog(@"array index 10 :%@", v);
NSMutableArray *array = [NSMutableArray array];
[array addObject:nil];
NSLog(@"NSMutableArray:%@", array);
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject:nil forKey:@"key1"];
[dict setObject:@"value1" forKey:nil];
[dict setObject:@"value2" forKey:@"key2"];
NSLog(@"NSMutableDictionary:%@", dict);
我把這些整合發(fā)布到pod上去了薛训。
pod search GJNoCrash
Crash log 處理有兩種方式
#import "GJCrashLog.h"
//方法1 block方法
[[GJCrashLog manager] callbackCrashLog:^(NSString *crashLog) {
//處理crashlog
}];
//方法2 delegate方式
[GJCrashLog manager].delegate = self;
//GJCrashLog Delegate
-(void)callbackGJCrashLog:(NSString *)crashLog
{
//處理crashlog
}
github地址在這里