zoukankan      html  css  js  c++  java
  • iOS开发NSMutableArray数组越界处理

    #import "NSArray+CrashArray.h"
    #import <objc/runtime.h>
    @implementation NSObject (Until)
    
    - (void)swizzleMethod:(SEL)originalSelector swizzledSelector:(SEL)swizzledSelector{
        Class class = [self class];
        Method original = class_getInstanceMethod(class, originalSelector);
        Method Swizzl   = class_getInstanceMethod(class, swizzledSelector);
        
        BOOL didAdd = class_addMethod(class, originalSelector, method_getImplementation(Swizzl), method_getTypeEncoding(Swizzl));
        if (didAdd) {
            class_replaceMethod(class, swizzledSelector, method_getImplementation(original), method_getTypeEncoding(original));
        }else{
            method_exchangeImplementations(original, Swizzl);
        }
    }
    
    @end
    @implementation NSArray (CrashArray)
    
    
    - (id)safeObjectAtIndex:(NSUInteger)index{
        if (index<self.count) {
            return [self safeObjectAtIndex:index];
        }else{
    //#ifdef DEBUG
    //        NSAssert(NO, @"index %lu > count %lu",(unsigned long)index,(unsigned long)self.count);
    //#endif
            return nil;
        }
    }
    - (id)safeObjectAtIndex1:(NSUInteger)index{
        if (index<self.count) {
            return [self safeObjectAtIndex1:index];
        }else{
    //#ifdef DEBUG
    //        NSAssert(NO, @"index %lu > count %lu",(unsigned long)index,(unsigned long)self.count);
    //#endif
            return nil;
        }
    }
    - (id)safeObjectAtIndex2:(NSUInteger)index{
        if (index<self.count) {
            return [self safeObjectAtIndex2:index];
        }else{
    //#ifdef DEBUG
    //        NSAssert(NO, @"index %lu > count %lu",(unsigned long)index,(unsigned long)self.count);
    //#endif
            return nil;
        }
    }
    - (id)safeObjectAtIndex3:(NSUInteger)index{
        if (index<self.count) {
            return [self safeObjectAtIndex3:index];
        }else{
    //#ifdef DEBUG
    //        NSAssert(NO, @"index %lu > count %lu",(unsigned long)index,(unsigned long)self.count);
    //#endif
            return nil;
        }
    }
    
    + (void)load{
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            @autoreleasepool {
                [objc_getClass("__NSArrayI") swizzleMethod:@selector(objectAtIndex:) swizzledSelector:@selector(safeObjectAtIndex:)];
                [objc_getClass("__NSArrayI") swizzleMethod:@selector(objectAtIndexedSubscript:) swizzledSelector:@selector(safeObjectAtIndex1:)];
    
                [objc_getClass("__NSArrayM") swizzleMethod:@selector(objectAtIndex:) swizzledSelector:@selector(safeObjectAtIndex2:)];
                [objc_getClass("__NSArrayM") swizzleMethod:@selector(objectAtIndexedSubscript:) swizzledSelector:@selector(safeObjectAtIndex3:)];
            }
        });
    }
    @end
    @implementation NSDictionary(DictinaryCrash)
    
    
    - (void)mutableSetObject:(id)obj forKey:(NSString *)key{
        if (obj && key) {
            [self mutableSetObject:obj forKey:key];
        }
    }
    + (void)load{
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            @autoreleasepool{
                [objc_getClass("__NSDictionaryM") swizzleMethod:@selector(setObject:forKey:) swizzledSelector:@selector(mutableSetObject:forKey:)];
            }
        });
    }
    @end
  • 相关阅读:
    Excel 如何复制粘贴一整行
    如何修改文件的扩展名(后缀)
    中文乱码之myEclipse项目导入时中文乱码(待)
    如何在java中导入jar包
    如何在myEclipse中创建配置文件,比如:XXX.properties
    Postman安装教程
    API是什么?——回答:接口。(待)
    找回J2EE 之再学习打卡记录
    让外界可以访问电脑上的网站的几种方式——花生壳,域名,IIS(待)
    18、任务暂停挂起
  • 原文地址:https://www.cnblogs.com/xianfeng-zhang/p/9341325.html
Copyright © 2011-2022 走看看