ios 数组越界奔溃库_iOS Runtime方法欺骗解决数组越界等造成的程序崩溃问题

本文介绍了如何使用iOS Runtime机制来处理UITableView在不同系统版本的兼容问题,以及解决数组越界导致的程序崩溃。通过UITableView分类和Runtime交换方法,实现了对iOS 11及以上版本的自动适配。同时,利用Runtime对NSArray和NSMutableArray的objectAtIndex及objectAtIndexedSubscript方法进行拦截,避免因数组越界引起的程序闪退。
摘要由CSDN通过智能技术生成

在开发中由于个人的疏忽或前后台更新问题,可能造成数组越界或者JSON键值对的改变,由此不可避免的会造成程序直接闪退,在开发中我们直接修改即可,如果上线了该怎么处理呢?本文主要介绍用iOS Runtime机制,来解决iOS 11之后系统版本UITableView莫名下移,数组NSArray越界造成的问题。

1、UITableView多个系统兼容处理

现在iOS 系统最新的已经升到12了, 我的项目在开发和维护时间已经几年了,要同时兼容不同的系统版本,由于iOS11对一些动画和机制的改变,UITableView在iOS 11之后的系统中显示存在异常,是iOS 11中废止了ViewController的automaticallyAdjustsScrollViewInsets属性,所以顶部就多了一定的inset,处理方法是在ViewController中添加以下代码就OK了。

// iOS 11 处理

if (@available(iOS 11.0, *)){

self.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;

}

那么,问题来了,我的工程中有几百个类中用到了UItableView,那么该如何处理呢?

1、可供选择方案

(1)、每个用到了UITableView的地方都做修改;

-(ListView *)listView

{

if(_listView == nil)

{

_listView = [[ListView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];

[self.view addSubview:_listView];

[_listView mas_makeConstraints:^(MASConstraintMaker *make) {

//设置上,左,下,右的距离

make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(0, 0, 0, 0));

}];

_listView.separatorStyle = UITableViewCellSeparatorStyleNone;

_listView.separatorInset = UIEdgeInsetsMake(1,0, 1, 0);

_listView.backgroundColor = [UIColor groupTableViewBackgroundColor];

// iOS 11 处理

if (@available(iOS 11.0, *)){

self.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;

}

}

return _listView;

}

这个方法确实可行,但是一个类一个类的改太费时间和精力,直接pass。

(2)、所有的ViewController都继承自一个基类BaseViewController,在基类中做处理;

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view.

// iOS 11 处理

if (@available(iOS 11.0, *)){

self.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;

}

}

如果在基类中操作,确实节省了不少工作量,但是我的代码中一些UITableView是写在在View层中,这样我还是要一个类一个类的修改,又回到了第一种方案中,那么有么有一种完美的解决方案呢?

(3)、UITableView分类+Runtime来实现

1、首先对系统的 UITableView做分类,命名为UITableView+Add.h;

2、自定义初始化方法initWithFrame,命名为initWithtableviewFrame在自定义的方法中对iOS 11做适配处理;

-(instancetype)initWithtableviewFrame:(CGRect)frame style:(UITableViewStyle)style

{

//此处不是自己调自己,是调系统的initWithFrame:方法,之前已经做交换了

self = [self initWithtableviewFrame:frame style:style];

if (self){

// iOS 11 处理

[self dealwithdosomething];

}

return self;

}

-(void)dealwithdosomething

{

// iOS 11 处理

if (@available(iOS 11.0, *)){

self.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;

}

}

3、在load方法中,(此处请自行百度load和initialize的区别),通过Runtime的method_exchangeImplementations函数通过把系统的initWithFrame方法和initWithtableviewFrame方法的IPM指针交换;

+(void)load

{

//获取两个方法的Method,参考Runtime默认写法

//class_getClassMethod 获取类方法

//class_getInstanceMethod 获取实例方法

Method systemAlloc = class_getInstanceMethod([UITableView class], @selector(initWithFrame:style:));

Method myAlloc = class_getInstanceMethod([UITableView class], @selector(initWithtableviewFrame:style:));

//实现交换

method_exchangeImplementations(systemAlloc, myAlloc);

}

2、利用Runtime解决数组越界造成的闪退问题

同样的,通过分类的方式对NSArray的load方法中做一些处理,即可避免由系统越界造成的闪退问题,核心代码如下:

#import

+ (void)load{

[super load];

//---------------------- 不可变数组 objectAtIndex

Method oldObjectAtIndex = class_getInstanceMethod(objc_getClass("__NSArrayI"), @selector(objectAtIndex:));

Method newObjectAtIndex = class_getInstanceMethod(objc_getClass("__NSArrayI"), @selector(newObjectAtIndex:));

method_exchangeImplementations(oldObjectAtIndex, newObjectAtIndex);

//---------------------- 不可变数组 []

Method oldMutableObjectAtIndex = class_getInstanceMethod(objc_getClass("__NSArrayI"), @selector(objectAtIndexedSubscript:));

Method newMutableObjectAtIndex = class_getInstanceMethod(objc_getClass("__NSArrayI"), @selector(newObjectAtIndexedSubscript:));

method_exchangeImplementations(oldMutableObjectAtIndex, newMutableObjectAtIndex);

//---------------------- 可变数组 objectAtIndex

Method oldMObjectAtIndex = class_getInstanceMethod(objc_getClass("__NSArrayM"), @selector(objectAtIndex:));

Method newMObjectAtIndex = class_getInstanceMethod(objc_getClass("__NSArrayM"), @selector(newMutableObjectAtIndex:));

method_exchangeImplementations(oldMObjectAtIndex, newMObjectAtIndex);

//---------------------- 可变数组 []

Method oldMMutableObjectAtIndex = class_getInstanceMethod(objc_getClass("__NSArrayM"), @selector(objectAtIndexedSubscript:));

Method newMMutableObjectAtIndex = class_getInstanceMethod(objc_getClass("__NSArrayM"), @selector(newMutableObjectAtIndexedSubscript:));

method_exchangeImplementations(oldMMutableObjectAtIndex, newMMutableObjectAtIndex);

}

// MARK: - 不可变数组 objectAtIndex

- (id)newObjectAtIndex:(NSUInteger)index{

if (index > self.count - 1 || !self.count){

@try {

return [self newObjectAtIndex:index];

} @catch (NSException *exception) {

NSLog(@"\n\n------------不可变数组越界了");

return nil;

} @finally {

}

}

else{

return [self newObjectAtIndex:index];

}

}

// MARK: - 不可变数组 []

- (id)newObjectAtIndexedSubscript:(NSUInteger)index{

if (index > self.count - 1 || !self.count){

@try {

return [self newObjectAtIndexedSubscript:index];

} @catch (NSException *exception) {

NSLog(@"\n\n------------不可变数组越界了");

return nil;

} @finally {

}

}

else{

return [self newObjectAtIndexedSubscript:index];

}

}

// MARK: - 可变数组 objectAtIndex

- (id)newMutableObjectAtIndex:(NSUInteger)index{

if (index > self.count - 1 || !self.count){

@try {

return [self newMutableObjectAtIndex:index];

} @catch (NSException *exception) {

NSLog(@"\n\n------------可变数组越界了");

return nil;

} @finally {

}

}

else{

return [self newMutableObjectAtIndex:index];

}

}

// MARK: - 可变数组 []

- (id)newMutableObjectAtIndexedSubscript:(NSUInteger)index{

if (index > self.count - 1 || !self.count){

@try {

return [self newMutableObjectAtIndexedSubscript:index];

} @catch (NSException *exception) {

NSLog(@"\n\n------------可变数组越界了");

return nil;

} @finally {

}

}

else{

return [self newMutableObjectAtIndexedSubscript:index];

}

}

来做一个测试:

NSArray *listAry = @[@1,@2,@3,@4,@5,@6,@7];

id a = listAry[10];

id b = [listAry objectAtIndex:10];

NSMutableArray *mutListAry = [NSMutableArray arrayWithArray:listAry];

id c = mutListAry[10];

id d = [mutListAry objectAtIndexedSubscript:10];

id e = [mutListAry objectAtIndex:10];

------------不可变数组越界了

------------不可变数组越界了

------------可变数组越界了

------------可变数组越界了

------------可变数组越界了

运行代码,可以看到Log,但APP依旧正常运行,没有闪退,这样就达到了目的。

通过Runtime来处理,不需要在之前的代码做任何修改,只需添加一个分类就解决了问题,岂不美哉。

本文demo请戳:GitHub

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值