iOS中防止数组越界之后发生崩溃
2017-02-20 11:48
471 查看
在iOS开发中有时会遇到数组越界的问题,从而导致程序崩溃。为了防止程序崩溃,我们就要对数组越界进行处理。通过上网查资料,发现可以通过为数组写一个分类来解决此问题。
基本思路:为NSArray写一个防止数组越界的分类。分类中利用runtime将系统中NSArray的对象方法objectAtIndex:替换,然后对objectAtIndex:传递过来的下标进行判断,如果发生数组越界就返回nil,如果没有发生越界,就继续调用系统的objectAtIndex:方法。
代码:
.h文件:
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
@interface NSArray (beyond)
@end
.m文件:
#import "NSArray+beyond.h"
@implementation NSArray (beyond)
+ (void)load{
[superload];
// 替换不可变数组中的方法
Method oldObjectAtIndex =class_getInstanceMethod(objc_getClass("__NSArrayI"),@selector(objectAtIndex:));
Method newObjectAtIndex =class_getInstanceMethod(objc_getClass("__NSArrayI"),@selector(__nickyTsui__objectAtIndex:));
method_exchangeImplementations(oldObjectAtIndex, newObjectAtIndex);
// 替换可变数组中的方法
Method oldMutableObjectAtIndex =class_getInstanceMethod(objc_getClass("__NSArrayM"),@selector(objectAtIndex:));
Method newMutableObjectAtIndex = class_getInstanceMethod(objc_getClass("__NSArrayM"),@selector(mutableObjectAtIndex:));
method_exchangeImplementations(oldMutableObjectAtIndex, newMutableObjectAtIndex);
}
- (id)__nickyTsui__objectAtIndex:(NSUInteger)index{
if (index >self.count -1
|| !self.count){
@try {
return [self__nickyTsui__objectAtIndex:index];
} @catch (NSException *exception) {
//__throwOutException 抛出异常
NSLog(@"数组越界...");
returnnil;
} @finally {
}
}
else{
return [self__nickyTsui__objectAtIndex:index];
}
}
- (id)mutableObjectAtIndex:(NSUInteger)index{
if (index >self.count -1
|| !self.count){
@try {
return [selfmutableObjectAtIndex:index];
} @catch (NSException *exception) {
//__throwOutException 抛出异常
NSLog(@"数组越界...");
returnnil;
} @finally {
}
}
else{
return [selfmutableObjectAtIndex:index];
}
}
@
基本思路:为NSArray写一个防止数组越界的分类。分类中利用runtime将系统中NSArray的对象方法objectAtIndex:替换,然后对objectAtIndex:传递过来的下标进行判断,如果发生数组越界就返回nil,如果没有发生越界,就继续调用系统的objectAtIndex:方法。
代码:
.h文件:
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
@interface NSArray (beyond)
@end
.m文件:
#import "NSArray+beyond.h"
@implementation NSArray (beyond)
+ (void)load{
[superload];
// 替换不可变数组中的方法
Method oldObjectAtIndex =class_getInstanceMethod(objc_getClass("__NSArrayI"),@selector(objectAtIndex:));
Method newObjectAtIndex =class_getInstanceMethod(objc_getClass("__NSArrayI"),@selector(__nickyTsui__objectAtIndex:));
method_exchangeImplementations(oldObjectAtIndex, newObjectAtIndex);
// 替换可变数组中的方法
Method oldMutableObjectAtIndex =class_getInstanceMethod(objc_getClass("__NSArrayM"),@selector(objectAtIndex:));
Method newMutableObjectAtIndex = class_getInstanceMethod(objc_getClass("__NSArrayM"),@selector(mutableObjectAtIndex:));
method_exchangeImplementations(oldMutableObjectAtIndex, newMutableObjectAtIndex);
}
- (id)__nickyTsui__objectAtIndex:(NSUInteger)index{
if (index >self.count -1
|| !self.count){
@try {
return [self__nickyTsui__objectAtIndex:index];
} @catch (NSException *exception) {
//__throwOutException 抛出异常
NSLog(@"数组越界...");
returnnil;
} @finally {
}
}
else{
return [self__nickyTsui__objectAtIndex:index];
}
}
- (id)mutableObjectAtIndex:(NSUInteger)index{
if (index >self.count -1
|| !self.count){
@try {
return [selfmutableObjectAtIndex:index];
} @catch (NSException *exception) {
//__throwOutException 抛出异常
NSLog(@"数组越界...");
returnnil;
} @finally {
}
}
else{
return [selfmutableObjectAtIndex:index];
}
}
@
相关文章推荐
- ios 使用运行时规避数组等越界导致程序崩溃
- 防止NSDictionary参数缺少发生意外崩溃的解决方法 iOS
- iOS 防止数组越界 对象类型不一致的安全处理 使用category对nsmutablearray处理
- iOS之利用runtime,避免可变数组和可变字典为nil或者数组越界导致的崩溃
- Objective-c防止数组越界而崩溃(全局效果)
- ios 11以上系统,tableview上啦刷新,indexPath.row,自动加5,引起数组越界的问题导致崩溃
- [iOS]防止数组越界及添加空值的crash的扩展
- Objective-c防止数组越界而崩溃(全局效果)
- 工具类:防止数组越界崩溃问题(NSArray 分类/runtime 用法3:交换方法)
- iOS - 数组,字典打印以及防止数组取值越界字典插入nil
- 数组越界导致的运行崩溃(枚举作为数组中的常量)
- input 输入加了 背景图片之后,防止 默认式样 发生变化的方法
- 数组越界之后
- iOS 多线程下 对数组 遍历,防止边读边写
- 用Method Swizzling来避免数组越界与可变字典value值为nil造成的程序崩溃
- iOS 关于数组越界的解决方法
- 数组越界场景集合(什么情况会出现数组越界呢)--iOS开发
- IOS-网络请求数据解析道数组程序崩溃问题
- 防止字典参数缺少发生意外崩溃的解决方法
- C++中将数组作为形参时防止访问越界的几种方法