关于数组越界的系统崩溃的问题
小伙伴门经常见到这个崩溃日志吧!! 能不能不不让系统不直接崩溃,进而影响用户体验!!!研究了下runtime 机制可以完美解决这个问题!让它不会因为数组越界而崩溃,并且抓取报错的堆栈信息~~~~🙄🙄 个人 建议:当我们在测试环境下最好不要用这个!!因为尽量减少奔溃的问题,并修复~~~~ 对那些已经是正式环境下可以用一下,不会因为隐藏在深处的问题导致系统直接崩溃,进而影响用户体验~~~~~~同时最好能上传报错日志,我们可以方便找到出错的地方并且修复! 我们公司用的是腾讯的Bugly 框架~~感兴趣的童鞋可以集成一下~~~😄 Bugly 飞机地址~~~
利用runtime 交换系统的方法 ~~~~ 实现思路:当我们对数组取值的时候会调用以下两种任意的一种的方法 !那么我们可以用runtime 的机制替换掉系统的方法,转换为我们自定义的方法,对自定义方法里面对数组进行判断!!!如果越界了 就抛出异常!
NSArray*arr =@[@"1",@"2"];
NSLog(@"%@",arr[10]); //这个方法就是 [arr objectAtIndexedSubscript : 10 ] 的缩写
NSLog(@"%@",[arr objectAtIndex:10]);
OK 下面贴出代码
创建一个NSObject 分类
/**
替换系统方法 所有的成员方法
@param originalSelector 系统的方法
@param swizzledSelector 自定义的方法
@return NO表示没有找到该方法
*/
+ (BOOL)swizzleMethod:(SEL)originalSelector withMethod:(SEL)swizzledSelector
{
MethodoriginalMethod =class_getInstanceMethod(self, originalSelector);
if(!originalMethod) {
NSString*string = [NSStringstringWithFormat:@"系统的: %@ 类没有找到 %@ 方法",NSStringFromClass([selfclass]),NSStringFromSelector(originalSelector)];
NSLog(@"%@",string);
returnNO;
}
MethodswizzledMethod =class_getInstanceMethod(self, swizzledSelector);
if(!swizzledMethod) {
NSString*string = [NSStringstringWithFormat:@"自定义: %@ 类没有找到 %@ 方法",NSStringFromClass([selfclass]),NSStringFromSelector(swizzledSelector)];
NSLog(@"%@",string);
returnNO;
}
if(class_addMethod(self, originalSelector,method_getImplementation(swizzledMethod),method_getTypeEncoding(swizzledMethod)))
{
class_replaceMethod(self, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
}else{
method_exchangeImplementations(originalMethod, swizzledMethod);
}
return YES;
}
/**
替换系统方法
@param originalSelector 系统的方法
@param iswizzleMethod YES 表示系统方法 为类方法 ; 表示成员方法
@param swizzledSelector 自定义的方法
@param iswithMethod YES 表示自定义方法 为类方法 ; 表示成员方法
@return NO表示没有找到该方法
*/
+ (BOOL)swizzleMethod:(SEL)originalSelector IswizzleMethod:(BOOL)iswizzleMethod withMethod:(SEL)swizzledSelector IswithMethod:(BOOL)iswithMethod
{
// class_getClassMethod 为类方法 class_getInstanceMethod 表示成员方法
MethodoriginalMethod = iswizzleMethod==YES?class_getClassMethod(self, originalSelector):class_getInstanceMethod(self, originalSelector);
if(!originalMethod) {
NSString*string = [NSStringstringWithFormat:@"系统的: %@ 类没有找到 %@ 方法",NSStringFromClass([selfclass]),NSStringFromSelector(originalSelector)];
NSLog(@"%@",string);
returnNO;
}
MethodswizzledMethod = iswithMethod==YES?class_getClassMethod(self, swizzledSelector):class_getInstanceMethod(self, swizzledSelector);
if(!swizzledMethod) {
NSString*string = [NSStringstringWithFormat:@"自定义: %@ 类没有找到 %@ 方法",NSStringFromClass([selfclass]),NSStringFromSelector(swizzledSelector)];
NSLog(@"%@",string);
returnNO;
}
if(class_addMethod(self, originalSelector,method_getImplementation(swizzledMethod),method_getTypeEncoding(swizzledMethod)))
{
class_replaceMethod(self, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
}else{
method_exchangeImplementations(originalMethod, swizzledMethod);
}
return YES;
}
新建个NSArray 分类 .m实现
+ (void)load
{
//替换不可变数组方法 取数组下标时应该替换 swizzleMethod 为系统的方法 withMethod 为自定义方法
[objc_getClass("__NSArrayI")swizzleMethod:@selector(objectAtIndexedSubscript:)withMethod:@selector(FJL_ObjectAtIndexedSubscript:)];
//替换不可变数组只有一个元素的时候
[objc_getClass("__NSSingleObjectArrayI")swizzleMethod:@selector(objectAtIndex:)withMethod:@selector(FJL_objectAtIndex:)];
//替换可变数组方法 取数组下标时应该替换
[objc_getClass("__NSArrayM")swizzleMethod:@selector(objectAtIndexedSubscript:)withMethod:@selector(FJL_mutableObjectAtIndexedSubscript:)];
//替换可变数组方法
[objc_getClass("__NSArrayM")swizzleMethod:@selector(objectAtIndex:)withMethod:@selector(FJL_mutableobjectAtIndex:)];
}
- (id)FJL_objectAtIndex:(NSUInteger)index
{
if(index >self.count-1|| !self.count) {
@try{
return[selfFJL_objectAtIndex:index]; //注意:此处并没有递归操作. 因为交换了系统的方法
}
@catch(NSException *exception) {
NSLog(@"exception==== %@", exception); //异常信息
// 抛出异常方式
// [Bugly reportException:exception];//我上传的是腾讯的崩溃日志
returnnil;
}
}else{
return[self FJL_objectAtIndex:index]; //注意:此处并没有递归操作. 因为交换了系统的方法
}
}
- (id)FJL_mutableobjectAtIndex:(NSUInteger)index
{
if(index >self.count-1|| !self.count) {
@try{
return [self FJL_mutableobjectAtIndex:index]; //注意:此处并没有递归操作. 因为交换了系统的方法
}
@catch(NSException *exception) {
NSLog(@"exception====: %@", exception.reason);
// 抛出异常方式
// [Bugly reportException:exception];//上报崩溃日志
returnnil;
}
}else{
return [self FJL_mutableobjectAtIndex:index]; //注意:此处并没有递归操作. 因为交换了系统的方法
}
}
- (id)FJL_ObjectAtIndexedSubscript:(NSUInteger)index
{
if(index >self.count-1|| !self.count) {
@try{
return [self FJL_ObjectAtIndexedSubscript:index]; //注意:此处并没有递归操作. 因为交换了系统的方法
}
@catch(NSException *exception) {
NSLog(@"exception====: %@", exception.reason);
// 抛出异常方式
// [Bugly reportException:exception];//上报崩溃日志
returnnil;
}
}else{
return [self FJL_ObjectAtIndexedSubscript:index]; //注意:此处并没有递归操作. 因为交换了系统的方法
}
}
- (id)FJL_mutableObjectAtIndexedSubscript:(NSUInteger)index
{
if(index >self.count-1|| !self.count) {
@try{
return [self FJL_mutableObjectAtIndexedSubscript:index]; //注意:此处并没有递归操作. 因为交换了系统的方法
}
@catch(NSException *exception) {
NSLog(@"exception====: %@", exception.reason);
// [Bugly reportException:exception];//上报崩溃日志
returnnil;
}
}else{
return [self FJL_mutableObjectAtIndexedSubscript:index]; //注意:此处并没有递归操作. 因为交换了系统的方法
}
}
OK 以上就是所有的源码 用着有问题欢迎@我哈