oc项目中使用Runtime的方法交换防止数组越界

import "NSArray+Demo.h"

import <objc/message.h>

@implementation NSArray (Demo)

  • (void)load{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
    @autoreleasepool {
    [self exchangeMethodWithClass:objc_getClass("__NSArray0") orginSelector:@selector(objectAtIndex:) customSelector:@selector(emptyObjectIndex:)];
    [self exchangeMethodWithClass:objc_getClass("__NSSingleObjectArrayI") orginSelector:@selector(objectAtIndex:) customSelector:@selector(singleArrObjectIndex:)];
    [self exchangeMethodWithClass:objc_getClass("__NSArrayI") orginSelector:@selector(objectAtIndex:) customSelector:@selector(arrObjectIndex:)];
    [self exchangeMethodWithClass:objc_getClass("__NSArrayM") orginSelector:@selector(objectAtIndex:) customSelector:@selector(mutableObjectIndex:)];
    [self exchangeMethodWithClass:objc_getClass("__NSArrayM") orginSelector:@selector(insertObject:atIndex:) customSelector:@selector(mutableInsertObject:atIndex:)];
    }
    });
    }

  • (void)exchangeMethodWithClass:(Class)class orginSelector:(SEL)orginS customSelector:(SEL)customS{
    Method orginMethod = class_getInstanceMethod(class, orginS);
    Method customMethod = class_getInstanceMethod(class, customS);
    method_exchangeImplementations(orginMethod, customMethod);
    }

  • (id)emptyObjectIndex:(NSInteger)index{
    return nil;
    }

  • (id)singleArrObjectIndex:(NSInteger)index{
    @autoreleasepool {
    if (index >= self.count || index < 0) {
    return nil;
    }
    return [self singleArrObjectIndex:index];
    }
    }

  • (id)arrObjectIndex:(NSInteger)index{
    @autoreleasepool {
    if (index >= self.count || index < 0) {
    return nil;
    }
    return [self arrObjectIndex:index];
    }
    }

  • (id)mutableObjectIndex:(NSInteger)index{
    @autoreleasepool {
    if (index >= self.count || index < 0) {
    return nil;
    }
    return [self mutableObjectIndex:index];
    }
    }

  • (void)mutableInsertObject:(id)object atIndex:(NSUInteger)index{
    @autoreleasepool {
    if (object) {
    [self mutableInsertObject:object atIndex:index];
    }
    }
    }

  • (id)myObjectAtIndex:(NSUInteger)index{
    @autoreleasepool {
    if (index < self.count) {
    return [self myObjectAtIndex:index];
    } else {
    return nil;
    }
    }
    }

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