美文网首页
2018-05-09

2018-05-09

作者: 九林 | 来源:发表于2018-05-09 16:17 被阅读87次

                                                                                    关于数组越界的系统崩溃的问题


小伙伴门经常见到这个崩溃日志吧!!  能不能不不让系统不直接崩溃,进而影响用户体验!!!研究了下runtime 机制可以完美解决这个问题!让它不会因为数组越界而崩溃,并且抓取报错的堆栈信息~~~~🙄🙄  个人 建议:当我们在测试环境下最好不要用这个!!因为尽量减少奔溃的问题,并修复~~~~  对那些已经是正式环境下可以用一下,不会因为隐藏在深处的问题导致系统直接崩溃,进而影响用户体验~~~~~~同时最好能上传报错日志,我们可以方便找到出错的地方并且修复!  我们公司用的是腾讯的Bugly 框架~~感兴趣的童鞋可以集成一下~~~😄  Bugly 飞机地址~~~

数组越界再熟悉不过了

利用runtime 交换系统的方法 ~~~~     实现思路:当我们对数组取值的时候会调用以下两种任意的一种的方法 !那么我们可以用runtime 的机制替换掉系统的方法,转换为我们自定义的方法,对自定义方法里面对数组进行判断!!!如果越界了 就抛出异常!

    NSArray*arr =@[@"1",@"2"];

    NSLog(@"%@",arr[10]);  //这个方法就是 [arr  objectAtIndexedSubscript : 10 ]  的缩写

    NSLog(@"%@",[arr objectAtIndex:10]);

OK 下面贴出代码


创建一个NSObject 分类

.h实现

/**

 替换系统方法 所有的成员方法

 @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  以上就是所有的源码    用着有问题欢迎@我哈


相关文章

  • 2018-05-09

    2018-05-09

  • 2018-05-09

    2018-05-09 戴师傅简书作者 2018-05-09 21:10 打开App (稻盛哲学学习会)打卡第54天...

  • selenium+js处理display属性为none

    Author='jolting' Date='2018-05-09' 故事and事故 故事是这样开始的,正在写自动...

  • Gone with the wind

    2018-05-09 周三 早上晨练,看到满草坪的蒲公英,心里升腾起柔...

  • 520情话-我必生死相依

    浅水涅槃 2018-05-09 10:07 · 字数 7324 · 阅读 96 · 原谅我喜欢你好多年 261 遇...

  • TF1-1第1次

    【时间】2018-05-09 【地点】福田锦囊寓酒店 【级别】TF1-1第1次 【自我感受】 【钻石反馈】 ...

  • 日精进打卡(第306天)

    2018-05-09 姓名:李义 公司:........ 组别:259期利他二组 【知~学习】 背诵 六项精进大纲...

  • neo4安装

    2018-05-09 安装 1.当然是下载了 neo4j-desktop-offline-1.1.0-x86_64...

  • selenium处理鼠标事件

    author='jolting' date='2018-05-09' 好久没更了,最近有项目测试,需要写自动化用例...

  • 详细深刻快速改命转运忏悔文

    详细深刻快速改命转运忏悔文 欧阳岳灵 已关注 2018-05-09 16:12 · 字数 717 · 阅读 90 ...

网友评论

      本文标题:2018-05-09

      本文链接:https://www.haomeiwen.com/subject/kaonrftx.html