跟踪记录线上常见崩溃

@implementation NSArray (LXZArray)

+ (void)load {
     [ super  load];
     Method fromMethod = class_getInstanceMethod(objc_getClass( "__NSArrayI" ), @selector(objectAtIndex:));
     Method toMethod = class_getInstanceMethod(objc_getClass( "__NSArrayI" ), @selector(lxz_objectAtIndex:));
     method_exchangeImplementations(fromMethod, toMethod);
}
 
- (id)lxz_objectAtIndex:(NSUInteger)index {
     if  (self.count-1 < index) {
         // 这里做一下异常处理,不然都不知道出错了。
         @ try  {
             return  [self lxz_objectAtIndex:index];
         }
         @ catch  (NSException *exception) {
             // 在崩溃后会打印崩溃信息,方便我们调试。
             NSLog(@ "---------- %s Crash Because Method %s  ----------\n" , class_getName(self.class), __func__);
             NSLog(@ "%@" , [exception callStackSymbols]);
        //  将以上信息通过接口上传或者自定义error上传到bugly。可达到跟踪目的
             return  nil;
     }
         @finally {}
     else  {
         return  [self lxz_objectAtIndex:index];
     }
}
@end

猜你喜欢

转载自www.cnblogs.com/diyigechengxu/p/9219713.html