一:崩溃 异常处理(NSSetUncaughtExceptionHandler)在macOS上没有效果。
NSArray *arr = @[@(0),@(1)];
NSLog(@"%@",arr[2]);//模拟越界异常
dispatch_async(dispatch_get_main_queue(), ^{
NSArray *testArray = [NSArray arrayWithObjects:@"test",@"test1", nil];
NSLog(@"%@", testArray[10]);
});
以上代码第一句不会走到
NSSetUncaughtExceptionHandler(&WMExceptionHandler);
void WMExceptionHandler(NSException *exception){
//获取系统当前时间,(注:用[NSDate date]直接获取的是格林尼治时间,有时差)
NSDateFormatter *formatter =[[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *crashTime = [formatter stringFromDate:[NSDate date]];
[formatter setDateFormat:@"HH-mm-ss"];
NSString *crashTimeStr = [formatter stringFromDate:[NSDate date]];
[formatter setDateFormat:@"yyyyMMdd"];
NSArray *arr = [exception callStackSymbols];
NSString *reason = [exception reason];
NSString *name = [exception name];
NSLog(@"\n%@\n%@\n%@",arr,reason,name);
NSLog(@"%s",__func__);
//获取堆栈信息
//NSArray * callStack = [WMUncaughtExceptionHandler l]
NSMutableDictionary *userInfo = [NSMutableDictionary dictionaryWithDictionary:[exception userInfo]];
[userInfo setObject:exception.name forKey:@""];
[userInfo setObject:exception.reason forKey:@""];
[userInfo setObject:exception.callStackSymbols forKey:@""];
[userInfo setObject:@"WMException" forKey:@""];
}
WMExceptionHandler中的。但是第二句代码就可以。
找了好久发现这个是一个Appkit系统级的bug.
参考如下:苹果官方
解决方案可以参考:
暂定解决方案