美文网首页
iOS数组越界的保护

iOS数组越界的保护

作者: dequal | 来源:发表于2018-09-17 12:18 被阅读0次

先上一段代码

    NSArray *testArray = @[@"a",@"b",@"c"];
    NSLog(@"%@",[testArray objectAtIndex:3]);
    NSLog(@"%@",testArray[3]);

在iOS中, 上述两种取数组元素的方法都会导致程序崩溃, 称为"数组越界", 那么在日常开发中,因为数据的不确定性, 如果每次取指定index的元素前都要对数组的count进行判断,会非常繁琐, 也会增加代码量.

这里介绍一种简单的全局方法, 是通过tuntime的method swizzling实现的:

为NSArray添加一个category

#import <Foundation/Foundation.h>

///对数组越界进行保护
// 适用于 objectAtIndex 的保护 ,对 array[index] 无效
// runtime实现, 无需导入头文件

@interface NSArray (DHArraySafe)

@end



#import "NSArray+ DHArraySafe.h"
#import <objc/runtime.h>

@implementation NSArray (DHArraySafe)

+ (void)load{
    [super load];
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{  //方法交换只要一次就好
        Method oldObjectAtIndex = class_getInstanceMethod(objc_getClass("__NSArrayI"), @selector(objectAtIndex:));
        Method newObjectAtIndex = class_getInstanceMethod(objc_getClass("__NSArrayI"), @selector(__nickyTsui__objectAtIndex:));
        method_exchangeImplementations(oldObjectAtIndex, newObjectAtIndex);
    });
}
- (id)__nickyTsui__objectAtIndex:(NSUInteger)index{
    if (index > self.count - 1 || !self.count){
        @try {
            return [self __nickyTsui__objectAtIndex:index];
        } @catch (NSException *exception) {
            //__throwOutException  抛出异常
            return nil;
        } @finally {
            
        }
    }
    else{
        return [self __nickyTsui__objectAtIndex:index];
    }
}
@end

//mutableArray的对象也需要做方法交换
@interface NSMutableArray (DHArraySafe)

@end
@implementation NSMutableArray (DHArraySafe)

+ (void)load{
    [super load];
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{  //方法交换只要一次就好
        Method oldObjectAtIndex = class_getInstanceMethod(objc_getClass("__NSArrayM"), @selector(objectAtIndex:));
        Method newObjectAtIndex = class_getInstanceMethod(objc_getClass("__NSArrayM"), @selector(__nickyTsui__objectAtIndex:));
        method_exchangeImplementations(oldObjectAtIndex, newObjectAtIndex);
    });
}
- (id)__nickyTsui__objectAtIndex:(NSUInteger)index{
    if (index > self.count - 1 || !self.count){
        @try {
            return [self __nickyTsui__objectAtIndex:index];
        } @catch (NSException *exception) {
            //__throwOutException  抛出异常
            return nil;
        } @finally {
            
        }
    }
    else{
        return [self __nickyTsui__objectAtIndex:index];
    }
}
@end

注意:

1.对数组越界进行保护
2.适用于 objectAtIndex 的保护 ,对 array[index] 无效
3.runtime实现, 无需导入头文件, 一次添加,对全局工程有效

相关文章

  • iOS数组越界的保护

    先上一段代码 在iOS中, 上述两种取数组元素的方法都会导致程序崩溃, 称为"数组越界", 那么在日常开发中,因为...

  • iOS--再也不用担心数组越界

    iOS--再也不用担心数组越界 iOS--再也不用担心数组越界

  • ios 对数组越界保护

    iOS数组越界会出现奔溃,对此,有两种解决方案,一种是使用NSArray类别,一种是使用runtime机制。 参考...

  • swift5.x NSArray NSMutableArray

    我们写oc的时候经常用到对数组越界的保护分类 OC的数组越界保护使用:[self.dataNameArry obj...

  • iOS 数组越界的处理和优化方案。

    iOS开发中最常见的列表数据,必须使用数组,但是使用数组总会出现数组越界的情况,下面列出三种优化数组越界的方式。 ...

  • ios开发之防数组越界

    ios开发中,不免会遇到数组越界的问题,而当数组越界时往往会导致程序的崩溃,结局的方法之一就是在数组的分类中使用r...

  • iOS 项目中swizzling应用

    1). swizzling源类 2). 崩溃保护类(数组越界,字典赋值崩溃保护) 3).导航栏返回按钮图片设置,...

  • iOS Crash三部曲~之二常见Crash

    1 越界 1.1 数组越界 第1类崩溃就是越界问题,最常见的就是数组越界。 崩溃信息: 分析: 可以看出当前数组的...

  • iOS防止数组越界

    可以直接hook数组的objectIndex方法进行判断,但是一定要记住,NSArray真正的类型是__NSArr...

  • iOS TableView 数组越界

    好久没写了。不应当的。 最近做项目碰到一个tableView老蹦的问题。 只要一刷新就蹦。上拉都不会。 让别人看也...

网友评论

      本文标题:iOS数组越界的保护

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