ios规避数组越界、字典空指针等崩溃(一)

查看bugly,好多崩溃由于莫名原因数组越界或者取超出数组范围内容,亦或set nil for  key 了,针对这些增加几个category来避免直接崩的情况,提升用户体验。

一、NSArray:

创建NSArray的扩展类,NSArray+Extension。重写load方法,通过runtime 交换方法来实现。

+(void)load

{


    staticdispatch_once_tonceToken;

    dispatch_once(&onceToken, ^{


        // objectindex

        MethodoldObjectAtIndex =class_getInstanceMethod(objc_getClass("__NSArrayI"),@selector(objectAtIndex:));

        MethodnewObjectAtIndex =class_getInstanceMethod(objc_getClass("__NSArrayI"),@selector(objectAtSafeIndex:));

        method_exchangeImplementations(oldObjectAtIndex, newObjectAtIndex);


        // arr[4]

        MethodoldObjectAtIndex1 =class_getInstanceMethod(objc_getClass("__NSArrayI"),@selector(objectAtIndexedSubscript:));

        MethodnewObjectAtIndex1 =  class_getInstanceMethod(objc_getClass("__NSArrayI"),@selector(safeobjectAtIndexedSubscript:));

        method_exchangeImplementations(oldObjectAtIndex1, newObjectAtIndex1);

    });

}


因为,数组取值可以用objectindex 和 arr[index]两种方式,所以需要这两种方法都需要替换。

- (id)objectAtSafeIndex:(NSUInteger)index

{

    if(index >self.count-1|| !self.count) {

        @try{

            return[selfobjectAtSafeIndex:index];

        }

        @catch(NSException *exception) {

            NSLog(@"exception: %@", exception.reason);

            returnnil;

        }


    }else{

        return[selfobjectAtSafeIndex:index];

    }

}

- (instancetype)safeobjectAtIndexedSubscript:(NSUInteger)index{

    if(index > (self.count-1)) {// 数组越界

        returnnil;

    }else{// 没有越界

        return [self safeobjectAtIndexedSubscript:index];

    }

}


二、NSMutableArray+Extension。。。同理,可变数组如下:

+(void)load  //第一次加载内存的时候会自动调用。

{



    staticdispatch_once_tonceToken;

    dispatch_once(&onceToken, ^{

        //addObject

        MethodorginalMethod =class_getInstanceMethod(NSClassFromString(@"__NSArrayM"),@selector(addObject:));

        MethodnewMethod =class_getInstanceMethod(NSClassFromString(@"__NSArrayM"),@selector(gp_addobjc:));

        method_exchangeImplementations(orginalMethod, newMethod);



        //替换objectAtIndex方法

        MethodoldMutableObjectAtIndex =class_getInstanceMethod(objc_getClass("__NSArrayM"),@selector(objectAtIndex:));

        MethodnewMutableObjectAtIndex =  class_getInstanceMethod(objc_getClass("__NSArrayM"),@selector(mutableObjectAtSafeIndex:));

        method_exchangeImplementations(oldMutableObjectAtIndex, newMutableObjectAtIndex);




        //替换arr[4]方法

        MethodoldMutableObjectAtIndex1 =class_getInstanceMethod(objc_getClass("__NSArrayM"),@selector(objectAtIndexedSubscript:));

        MethodnewMutableObjectAtIndex1 =  class_getInstanceMethod(objc_getClass("__NSArrayM"),@selector(safeobjectAtIndexedSubscript:));

        method_exchangeImplementations(oldMutableObjectAtIndex1, newMutableObjectAtIndex1);

    });



}

//addobject

-(void)gp_addobjc:(id)object

{

    if(object !=nil) {

        [self gp_addobjc:object];//此时  方法交换,所以不能在这写addobject。若这样的话,会造成死循环。

    }

}

//objectAtIndex

- (id)mutableObjectAtSafeIndex:(NSUInteger)index

{

    if(index >self.count-1|| !self.count) {

        @try{

            return [self mutableObjectAtSafeIndex:index];

        }

        @catch(NSException *exception) {

            NSLog(@"exception: %@", exception.reason);

            returnnil;

        }


    }else{

        return [self mutableObjectAtSafeIndex:index];

    }

}

//aar[index]

- (instancetype)safeobjectAtIndexedSubscript:(NSUInteger)index{

    if(index > (self.count-1)) {// 数组越界

        returnnil;

    }else{// 没有越界

        return [self safeobjectAtIndexedSubscript:index];

    }

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 转至元数据结尾创建: 董潇伟,最新修改于: 十二月 23, 2016 转至元数据起始第一章:isa和Class一....
    40c0490e5268阅读 1,774评论 0 9
  • 一、runtime之数据、字典越界 方法交换 Runtime解决数据越界及字典key或value为nil的情况,主...
    nenhall阅读 547评论 0 1
  • 关于数组越界目前大概有两种方式,一种是通过分类添加安全的索引方法,第二种就是Runtime实现,第一种如果是个人开...
    dongke阅读 578评论 0 0
  • 关于数组越界的系统崩溃的问题 小伙伴门经常见到这个崩溃日志吧!! 能不能不不让系统不直接崩溃,进而影响用户体验!!...
    九林阅读 380评论 1 1
  • ios开发中,不免会遇到数组越界的问题,而当数组越界时往往会导致程序的崩溃,结局的方法之一就是在数组的分类中使用r...
    飞扬的青春8780975阅读 981评论 5 2