ios 使用运行时规避数组等越界导致程序崩溃

通过runtime的method swizzling 交换方法的实现 提前判断方法的参数是否符合要求
#import "NSMutableArray+TonyRuntime.h"
#import <objc/runtime.h>
@implementation NSMutableArray (TonyRuntime)
+(void)load
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Method sourceMethod1 = class_getInstanceMethod(objc_getClass("__NSArrayM"), @selector(objectAtIndex:));
        Method destMethod1 = class_getInstanceMethod(objc_getClass("__NSArrayM"), @selector(safeObjectAtIndex:));
        
        method_exchangeImplementations(sourceMethod1, destMethod1);
        
        Method sourceMethod2 = class_getInstanceMethod(objc_getClass("__NSArrayM"), @selector(removeObjectAtIndex:));
        Method destMethod2 = class_getInstanceMethod(objc_getClass("__NSArrayM"), @selector(safeRemoveObjectAtIndex:));
        
        method_exchangeImplementations(sourceMethod2, destMethod2);
        
        Method sourceMethod3 = class_getInstanceMethod(objc_getClass("__NSArrayM"), @selector(insertObject:atIndex:));
        Method destMethod3 = class_getInstanceMethod(objc_getClass("__NSArrayM"), @selector(safeInsertObject:atIndex:));
        
        method_exchangeImplementations(sourceMethod3, destMethod3);
        
        Method sourceMethod4 = class_getInstanceMethod(objc_getClass("__NSArrayM"), @selector(replaceObjectAtIndex:withObject:));
        Method destMethod4 = class_getInstanceMethod(objc_getClass("__NSArrayM"), @selector(safeReplaceObjectAtIndex:withObject:));
        
        method_exchangeImplementations(sourceMethod4, destMethod4);
    });
}
#pragma mark - 数组的安全处理
-(instancetype)safeObjectAtIndex:(NSInteger)index
{
    if (self.count < (index + 1)) {
        NSLog(@"Runtime Warning:index %li out of bound",index);
        return nil;
    }
    
    return [self safeObjectAtIndex:index];
}
-(void)safeRemoveObjectAtIndex:(NSInteger)index
{
    if (self.count <= index) {
        NSLog(@"Runtime Warning:index %li out of bound",index);
        return;
    }
    
    [self safeRemoveObjectAtIndex:index];
}
-(void)safeInsertObject:(id)object atIndex:(NSInteger)index
{
    if (!object) {
        NSLog(@"Runtime Warning:insert object can not be nil");
        return;
    }
    
    if (self.count < index) {
        NSLog(@"Runtime Warning:insert object at index %li out of bound",index);
        return;
    }
    
    [self safeInsertObject:object atIndex:index];
}
-(void)safeReplaceObjectAtIndex:(NSInteger)index withObject:(id)object
{
    if (index >= self.count) {
        NSLog(@"Runtime Warning:index %li out of bound",index);
        return;
    }
    
    if (!object) {
        NSLog(@"Runtime Warning:object can not be empty");
        return;
    }
    
    [self safeReplaceObjectAtIndex:index withObject:object];
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值