在開(kāi)發(fā)中由于個(gè)人的疏忽或前后臺(tái)更新問(wèn)題,可能造成數(shù)組越界或者JSON鍵值對(duì)的改變,由此不可避免的會(huì)造成程序直接閃退,在開(kāi)發(fā)中我們直接修改即可,如果上線了該怎么處理呢?本文主要介紹用iOS Runtime機(jī)制,來(lái)解決iOS 11之后系統(tǒng)版本UITableView莫名下移,數(shù)組NSArray越界造成的問(wèn)題。
1、UITableView多個(gè)系統(tǒng)兼容處理
現(xiàn)在iOS 系統(tǒng)最新的已經(jīng)升到12了, 我的項(xiàng)目在開(kāi)發(fā)和維護(hù)時(shí)間已經(jīng)幾年了,要同時(shí)兼容不同的系統(tǒng)版本,由于iOS11對(duì)一些動(dòng)畫和機(jī)制的改變,UITableView在iOS 11之后的系統(tǒng)中顯示存在異常,是iOS 11中廢止了ViewController的automaticallyAdjustsScrollViewInsets屬性,所以頂部就多了一定的inset,處理方法是在ViewController中添加以下代碼就OK了。
// iOS 11 處理
if (@available(iOS 11.0, *)){
self.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
}
那么,問(wèn)題來(lái)了,我的工程中有幾百個(gè)類中用到了UItableView,那么該如何處理呢?
1、可供選擇方案
(1)、每個(gè)用到了UITableView的地方都做修改;
-(ListView *)listView
{
if(_listView == nil)
{
_listView = [[ListView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
[self.view addSubview:_listView];
[_listView mas_makeConstraints:^(MASConstraintMaker *make) {
//設(shè)置上,左,下,右的距離
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;
}
這個(gè)方法確實(shí)可行,但是一個(gè)類一個(gè)類的改太費(fèi)時(shí)間和精力,直接pass。
(2)、所有的ViewController都繼承自一個(gè)基類BaseViewController,在基類中做處理;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
// iOS 11 處理
if (@available(iOS 11.0, *)){
self.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
}
}
如果在基類中操作,確實(shí)節(jié)省了不少工作量,但是我的代碼中一些UITableView是寫在在View層中,這樣我還是要一個(gè)類一個(gè)類的修改,又回到了第一種方案中,那么有么有一種完美的解決方案呢?
(3)、UITableView分類+Runtime來(lái)實(shí)現(xiàn)
1、首先對(duì)系統(tǒng)的 UITableView做分類,命名為UITableView+Add.h;
2、自定義初始化方法initWithFrame,命名為initWithtableviewFrame在自定義的方法中對(duì)iOS 11做適配處理;
-(instancetype)initWithtableviewFrame:(CGRect)frame style:(UITableViewStyle)style
{
//此處不是自己調(diào)自己,是調(diào)系統(tǒng)的initWithFrame:方法,之前已經(jīng)做交換了
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方法中,(此處請(qǐng)自行百度load和initialize的區(qū)別),通過(guò)Runtime的method_exchangeImplementations函數(shù)通過(guò)把系統(tǒng)的initWithFrame方法和initWithtableviewFrame方法的IPM指針交換;
+(void)load
{
//獲取兩個(gè)方法的Method,參考Runtime默認(rèn)寫法
//class_getClassMethod 獲取類方法
//class_getInstanceMethod 獲取實(shí)例方法
Method systemAlloc = class_getInstanceMethod([UITableView class], @selector(initWithFrame:style:));
Method myAlloc = class_getInstanceMethod([UITableView class], @selector(initWithtableviewFrame:style:));
//實(shí)現(xiàn)交換
method_exchangeImplementations(systemAlloc, myAlloc);
}
2、利用Runtime解決數(shù)組越界造成的閃退問(wèn)題
同樣的,通過(guò)分類的方式對(duì)NSArray的load方法中做一些處理,即可避免由系統(tǒng)越界造成的閃退問(wèn)題,核心代碼如下:
#import <objc/runtime.h>
+ (void)load{
[super load];
//---------------------- 不可變數(shù)組 objectAtIndex
Method oldObjectAtIndex = class_getInstanceMethod(objc_getClass("__NSArrayI"), @selector(objectAtIndex:));
Method newObjectAtIndex = class_getInstanceMethod(objc_getClass("__NSArrayI"), @selector(newObjectAtIndex:));
method_exchangeImplementations(oldObjectAtIndex, newObjectAtIndex);
//---------------------- 不可變數(shù)組 []
Method oldMutableObjectAtIndex = class_getInstanceMethod(objc_getClass("__NSArrayI"), @selector(objectAtIndexedSubscript:));
Method newMutableObjectAtIndex = class_getInstanceMethod(objc_getClass("__NSArrayI"), @selector(newObjectAtIndexedSubscript:));
method_exchangeImplementations(oldMutableObjectAtIndex, newMutableObjectAtIndex);
//---------------------- 可變數(shù)組 objectAtIndex
Method oldMObjectAtIndex = class_getInstanceMethod(objc_getClass("__NSArrayM"), @selector(objectAtIndex:));
Method newMObjectAtIndex = class_getInstanceMethod(objc_getClass("__NSArrayM"), @selector(newMutableObjectAtIndex:));
method_exchangeImplementations(oldMObjectAtIndex, newMObjectAtIndex);
//---------------------- 可變數(shù)組 []
Method oldMMutableObjectAtIndex = class_getInstanceMethod(objc_getClass("__NSArrayM"), @selector(objectAtIndexedSubscript:));
Method newMMutableObjectAtIndex = class_getInstanceMethod(objc_getClass("__NSArrayM"), @selector(newMutableObjectAtIndexedSubscript:));
method_exchangeImplementations(oldMMutableObjectAtIndex, newMMutableObjectAtIndex);
}
// MARK: - 不可變數(shù)組 objectAtIndex
- (id)newObjectAtIndex:(NSUInteger)index{
if (index > self.count - 1 || !self.count){
@try {
return [self newObjectAtIndex:index];
} @catch (NSException *exception) {
NSLog(@"\n\n------------不可變數(shù)組越界了");
return nil;
} @finally {
}
}
else{
return [self newObjectAtIndex:index];
}
}
// MARK: - 不可變數(shù)組 []
- (id)newObjectAtIndexedSubscript:(NSUInteger)index{
if (index > self.count - 1 || !self.count){
@try {
return [self newObjectAtIndexedSubscript:index];
} @catch (NSException *exception) {
NSLog(@"\n\n------------不可變數(shù)組越界了");
return nil;
} @finally {
}
}
else{
return [self newObjectAtIndexedSubscript:index];
}
}
// MARK: - 可變數(shù)組 objectAtIndex
- (id)newMutableObjectAtIndex:(NSUInteger)index{
if (index > self.count - 1 || !self.count){
@try {
return [self newMutableObjectAtIndex:index];
} @catch (NSException *exception) {
NSLog(@"\n\n------------可變數(shù)組越界了");
return nil;
} @finally {
}
}
else{
return [self newMutableObjectAtIndex:index];
}
}
// MARK: - 可變數(shù)組 []
- (id)newMutableObjectAtIndexedSubscript:(NSUInteger)index{
if (index > self.count - 1 || !self.count){
@try {
return [self newMutableObjectAtIndexedSubscript:index];
} @catch (NSException *exception) {
NSLog(@"\n\n------------可變數(shù)組越界了");
return nil;
} @finally {
}
}
else{
return [self newMutableObjectAtIndexedSubscript:index];
}
}
來(lái)做一個(gè)測(cè)試:
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];
------------不可變數(shù)組越界了
------------不可變數(shù)組越界了
------------可變數(shù)組越界了
------------可變數(shù)組越界了
------------可變數(shù)組越界了
運(yùn)行代碼,可以看到Log,但APP依舊正常運(yùn)行,沒(méi)有閃退,這樣就達(dá)到了目的。
通過(guò)Runtime來(lái)處理,不需要在之前的代碼做任何修改,只需添加一個(gè)分類就解決了問(wèn)題,豈不美哉。
本文demo請(qǐng)戳:GitHub