#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. protobuf3的学习笔记

    学习protobuf的过程中踩了不少的坑,这篇博文算是一个小结吧! 环境: windows VisualStudio Google.Protobuf.Tools. Google.Protobuf. 其 ...

  2. vue在v-for循环中绑定v-model

    原始示例 <div v-for="item in items"> <input type="text" v-model="'good ...

  3. delphi xe10 手机程序事件服务操作、退出键操作

    //程序事件服务操作 var FMXApplicationEventService: IFMXApplicationEventService; begin if TPlatformServices.C ...

  4. JavaScript中的节流和防抖

    节流: 在规定时间内,多次触发事件,但是只执行一次 场景:输入框搜索,地图渲染 优化用户体验 /** * 节流 规定时间内不管触发多少次只执行一次 * @param {Function} fn 实际要 ...

  5. linux源码安装python及pip和django

    1安装编译工具 yum install zlib-devel bzip2-devel openssl-devel python-devel kernel-devel libffi-devel ncur ...

  6. android 插件化框架VitualAPK

    推荐阅读: 滴滴Booster移动App质量优化框架-学习之旅 一 Android 模块Api化演练 不一样视角的Glide剖析(一) LeakCanary 与 鹅场Matrix ResourceCa ...

  7. NX二次开发-UFUN获取圆柱的参数UF_MODL_ask_cylinder_parms

    NX11+VS2013 #include <uf.h> #include <uf_modl.h> #include <uf_ui.h> UF_initialize( ...

  8. 移动端多选插件-jquery

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  9. easyUI tabs 显示与隐藏 tab 页

    隐藏: tab_option = $('#tabs').tabs('getTab'," 单位信息 ").panel('options').tab; tab_option.hide( ...

  10. c++-文件分离

    实现文件分离 1.头文件中不要使用using namespace,由于c++编译的特性,由于初学还没深入了解,不做具体编译的解释 2.由于没有了命名空间,所以string定义要写成std::strin ...