#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

iOS开发NSMutableArray数组越界处理的更多相关文章

  1. IOS开发 清空数组正确方法

    NSArray以及NSMutableArray 在Objc中的两种数组(不可变数组和可变数组), 在日常开发中,经常会遇到需要清空数组的情况,很多人下意识的会想到nil这个方法,这里是不提倡的.因为如 ...

  2. iOS开发中数组常用的五种遍历方式

    随着iOS的不断发展,apple也不断推出性能更高的数组遍历方式,下面将对熟悉的五种遍历方式进行列举. 首先定义一个数组,并获取数组长度 NSArray *array=@[",]; NSIn ...

  3. iOS 开发--NSMutableArray使用枚举方法

    可变数组也可以使用枚举方法, 我们在这里提供了两种枚举方法, 一个是正序枚举, 一个是倒序枚举, 在正序枚举中, 元素的个数和顺序都是不可以修改的, 但是在倒序枚举中却可以修改, 这有些耐人寻味. 涉 ...

  4. ios开发之--数组的一些操作

    1,创建数组 NSMutableArray * array =[[NSMutableArray alloc] initWithObjects:@"a",@"b" ...

  5. iOS开发——根据数组中的字典中的某一元素排序

    数组中的元素是字典,字典中的某一个元素,比如说姓名,现在需要按照姓名的首字母来排序,怎么搞? 做法很简单,在字典中加一个元素,保存姓名的首字母,然后用下面的方法排序. - (void)sortWifi ...

  6. iOS开发——高级篇——iOS如何彻底避免数组越界

    我们先来看看有可能会出现的数组越界Crash的地方: ? 1 2 3 4 5 6 7 - (void)tableView:(UITableView *)tableView didSelectRowAt ...

  7. iOS之利用runtime,避免可变数组和可变字典为nil或者数组越界导致的崩溃

    NSArray.NSMutableArray.NSDictionary.NSMutableDictionary.是我们的在iOS开发中非常常用的类.当然,在享受这些类的便利的同时,它们也给我们带来一些 ...

  8. iOS阶段学习第15天笔记(NSArray与NSMutableArray 数组)

    iOS学习(OC语言)知识点整理 一.OC中的数组 1)数组:也是一个对象,数组中存放的是对象的地址,可以存放任意类型对象的地址,只能是对象不能是具体的数值,数组是有序的,      可以存放重复的元 ...

  9. iOS开发环境C语言基础 数组 函数

    1 求数组元素的最大值 1.1 问题 创建程序,实现查询数组中最大值的功能,需求为:创建一个长度为10的数组,数组内放置10个0~99之间(包含0,包含99)的随机数作为数组内容,要求查询出数组中的最 ...

随机推荐

  1. Jmeter-【JSON Extractor】-响应结果中三级key取值

    一.请求返回样式 二.取第三个option 三.查看结果

  2. JS For 循环详解;棋盘放粮食 64;冒泡排序实例

    FOR( 初始条件:循环条件:状态改变:) { 被执行的代码块} 初始条件: 在循环(代码块)开始前执行 循环条件:定义运行循环(代码块)的条件 状态改变: 在循环(代码块)已被执行之后执行 循环可以 ...

  3. NX二次开发-设置WCS位置UF_CSYS_set_wcs

    NX9+VS2012 UF_initialize(); //输入X向量Y向量输出一个3*3矩阵 ] = {0.0, 0.0, 1.0}; ] = {0.0, 1.0, 0.0}; ]; UF_MTX3 ...

  4. org.mybatis总是认不出来的原因,pom.xml中有

    idea打开maven project,然后就华丽丽的看见下载失败,怎么刷新都没有用. 请按以下步骤完成即可: https://blog.csdn.net/ZhengYanFeng1989/artic ...

  5. Codeforces 1174B Ehab Is an Odd Person

    题目链接:http://codeforces.com/problemset/problem/1174/B 题意:给定长度 n 的数组,任意俩个相加为奇数的数可以交换数组中的位置,让这个数组尽量从小到大 ...

  6. js用document.getElementById时要注意!

    <!DOCTYPE html> <html lang="en"> <head> <script src="http://code ...

  7. python学习4—数据结构之列表、元组与字典

    python学习4—数据结构之列表.元组与字典 列表(list)深灰魔法 1. 连续索引 li = [1,1,[1,["asdsa",4]]] li[2][1][1][0] 2. ...

  8. lambda x:i*x for i in range(4)

    解决方法:冒号前添加接收 i 的变量 return [lambda x,i=i: i * x for i in range(4)]

  9. 知识图谱+Recorder︱中文知识图谱API与工具、科研机构与算法框架

    目录 分为两个部分,笔者看到的知识图谱在商业领域的应用,外加看到的一些算法框架与研究机构. 文章目录 @ 一.知识图谱商业应用 01 唯品金融大数据 02 PlantData知识图谱数据智能平台 03 ...

  10. echarts数据变了不重新渲染,以及重新渲染了前后数据会重叠渲染的问题

    1.echarts数据变了但是视图不重新渲染 新建Chart.vue文件 <template>  <p :id="id" :style="style&q ...