美文网首页
iOS开发数组越界全局解决方案

iOS开发数组越界全局解决方案

作者: 叫我小黑 | 来源:发表于2018-10-23 10:13 被阅读0次

创建一个NSArray的类别添加到项目中,类别中使用 runtime 将系统的方法替换为你自己实现的方法

//.h
#import <Foundation/Foundation.h>
@interface NSArray (Beyond)
@end

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

@implementation NSArray (Beyond)

+ (void)load
{
    [super load];
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        //替换空数组方法
        [self class:objc_getClass("__NSArray0") exchangeImplementation:@selector(objectAtIndex:) withMethod:@selector(emptyObjectIndex:)];
        
        [self class:objc_getClass("__NSSingleObjectArrayI") exchangeImplementation:@selector(objectAtIndex:) withMethod:@selector(singleObjectIndex:)];
        
        //替换不可变数组方法
        [self class:objc_getClass("__NSArrayI") exchangeImplementation:@selector(objectAtIndex:) withMethod:@selector(objectAtSafeIndex:)];
        [self class:objc_getClass("__NSArrayI") exchangeImplementation:@selector(objectAtIndexedSubscript:) withMethod:@selector(objectAtSafeIndexSubscript:)];

        //替换可变数组方法
        [self class:objc_getClass("__NSArrayM") exchangeImplementation:@selector(objectAtIndex:) withMethod:@selector(mutableObjectAtSafeIndex:)];
        [self class:objc_getClass("__NSArrayM") exchangeImplementation:@selector(objectAtIndexedSubscript:) withMethod:@selector(mutableObjectAtSafeIndexSubscript:)];
        
        
        
    });
    
}

+ (void)class:(Class)class exchangeImplementation:(SEL)oldSEL withMethod:(SEL)newSEL
{
    Method oldMethod = class_getInstanceMethod(class, oldSEL);
    Method newMethod = class_getInstanceMethod(class, newSEL);
    method_exchangeImplementations(oldMethod, newMethod);
}

- (id)emptyObjectIndex:(NSUInteger)index
{
    NSLog(@"__NSArray0 取一个空数组 objectAtIndex , 崩溃") ;
    return nil;
}

- (id)singleObjectIndex:(NSInteger)index {
    
    if (index >= self.count || !self.count) {
        
        NSLog(@"__NSSingleObjectArrayI 取一个不可变单元素数组时越界 objectAtIndex , 崩溃") ;
        return nil;
        
    }
    
    return [self singleObjectIndex:index];
}


- (id)objectAtSafeIndex:(NSUInteger)index
{
    if (index > self.count - 1 || !self.count) {
        @try {
            return [self objectAtSafeIndex:index];
        }
        @catch (NSException *exception) {
            NSLog(@"exception: %@", exception.reason);
            return nil;
        }
        
    }else {
        return [self objectAtSafeIndex:index];
    }
}

- (id)objectAtSafeIndexSubscript:(NSUInteger)index
{
    if (index > self.count - 1 || !self.count) {
        @try {
            return [self objectAtSafeIndexSubscript:index];
        }
        @catch (NSException *exception) {
            NSLog(@"exception: %@", exception.reason);
            return nil;
        }
        
    }else {
        return [self objectAtSafeIndexSubscript:index];
    }
}

- (id)mutableObjectAtSafeIndex:(NSUInteger)index
{
    if (index > self.count - 1 || !self.count) {
        @try {
            return [self mutableObjectAtSafeIndex:index];
        }
        @catch (NSException *exception) {
            NSLog(@"exception: %@", exception.reason);
            return nil;
        }
        
    }else {
        return [self mutableObjectAtSafeIndex:index];
    }
}

- (id)mutableObjectAtSafeIndexSubscript:(NSUInteger)index
{
    if (index > self.count - 1 || !self.count) {
        @try {
            return [self mutableObjectAtSafeIndexSubscript:index];
        }
        @catch (NSException *exception) {
            NSLog(@"exception: %@", exception.reason);
            return nil;
        }
        
    }else {
        return [self mutableObjectAtSafeIndexSubscript:index];
    }
}

@end

相关文章

  • iOS开发数组越界全局解决方案

    创建一个NSArray的类别添加到项目中,类别中使用 runtime 将系统的方法替换为你自己实现的方法

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

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

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

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

  • ios开发之防数组越界

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

  • iOS array数组防越界

    iOS开发中常用到array数组,本文介绍一个防止数组越界的方法,分享给有需要的人;首先创建NSArray的Cat...

  • ios 对数组越界保护

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

  • iOS数组越界的保护

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

  • tableview 使用中防止数组越界

    在写项目的时候,调用下面的方法时 。 其实这是数组越界造成的:解决方案如下 增加了防止数组越界的判断,这句代码 i...

  • swift扩展类-Array

    在使用数组的时候,最常见的异常就是数组越界了,为了避免在开发的时候出现越界的情况,写了几个扩展的方法,用于缓减越界...

  • iOS开发中的数组越界问题

    我们在开发过程中可能会遇到一些数组越界的问题,当我们遇到这样的问题的时候我们可以为NSArray和NSMutabl...

网友评论

      本文标题:iOS开发数组越界全局解决方案

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