ios規(guī)避數(shù)組越界、字典空指針等崩潰(一)

查看bugly,好多崩潰由于莫名原因數(shù)組越界或者取超出數(shù)組范圍內(nèi)容,亦或set nil for ?key 了,針對(duì)這些增加幾個(gè)category來(lái)避免直接崩的情況,提升用戶體驗(yàn)。

一、NSArray:

創(chuàng)建NSArray的擴(kuò)展類,NSArray+Extension。重寫load方法,通過runtime 交換方法來(lái)實(shí)現(xiàn)。

+(void)load

{


? ? staticdispatch_once_tonceToken;

? ? dispatch_once(&onceToken, ^{


? ? ? ? // objectindex

? ? ? ? MethodoldObjectAtIndex =class_getInstanceMethod(objc_getClass("__NSArrayI"),@selector(objectAtIndex:));

? ? ? ? MethodnewObjectAtIndex =class_getInstanceMethod(objc_getClass("__NSArrayI"),@selector(objectAtSafeIndex:));

? ? ? ? method_exchangeImplementations(oldObjectAtIndex, newObjectAtIndex);


? ? ? ? // arr[4]

? ? ? ? MethodoldObjectAtIndex1 =class_getInstanceMethod(objc_getClass("__NSArrayI"),@selector(objectAtIndexedSubscript:));

? ? ? ? MethodnewObjectAtIndex1 =? class_getInstanceMethod(objc_getClass("__NSArrayI"),@selector(safeobjectAtIndexedSubscript:));

? ? ? ? method_exchangeImplementations(oldObjectAtIndex1, newObjectAtIndex1);

? ? });

}


因?yàn)?,?shù)組取值可以用objectindex 和 arr[index]兩種方式,所以需要這兩種方法都需要替換。

- (id)objectAtSafeIndex:(NSUInteger)index

{

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

? ? ? ? @try{

? ? ? ? ? ? return[selfobjectAtSafeIndex:index];

? ? ? ? }

? ? ? ? @catch(NSException *exception) {

? ? ? ? ? ? NSLog(@"exception: %@", exception.reason);

? ? ? ? ? ? returnnil;

? ? ? ? }


? ? }else{

? ? ? ? return[selfobjectAtSafeIndex:index];

? ? }

}

- (instancetype)safeobjectAtIndexedSubscript:(NSUInteger)index{

? ? if(index > (self.count-1)) {// 數(shù)組越界

? ? ? ? returnnil;

? ? }else{// 沒有越界

? ? ? ? return [self safeobjectAtIndexedSubscript:index];

? ? }

}


二、NSMutableArray+Extension。。。同理,可變數(shù)組如下:

+(void)load? //第一次加載內(nèi)存的時(shí)候會(huì)自動(dòng)調(diào)用。

{



? ? staticdispatch_once_tonceToken;

? ? dispatch_once(&onceToken, ^{

? ? ? ? //addObject

? ? ? ? MethodorginalMethod =class_getInstanceMethod(NSClassFromString(@"__NSArrayM"),@selector(addObject:));

? ? ? ? MethodnewMethod =class_getInstanceMethod(NSClassFromString(@"__NSArrayM"),@selector(gp_addobjc:));

? ? ? ? method_exchangeImplementations(orginalMethod, newMethod);



? ? ? ? //替換objectAtIndex方法

? ? ? ? MethodoldMutableObjectAtIndex =class_getInstanceMethod(objc_getClass("__NSArrayM"),@selector(objectAtIndex:));

? ? ? ? MethodnewMutableObjectAtIndex =? class_getInstanceMethod(objc_getClass("__NSArrayM"),@selector(mutableObjectAtSafeIndex:));

? ? ? ? method_exchangeImplementations(oldMutableObjectAtIndex, newMutableObjectAtIndex);




? ? ? ? //替換arr[4]方法

? ? ? ? MethodoldMutableObjectAtIndex1 =class_getInstanceMethod(objc_getClass("__NSArrayM"),@selector(objectAtIndexedSubscript:));

? ? ? ? MethodnewMutableObjectAtIndex1 =? class_getInstanceMethod(objc_getClass("__NSArrayM"),@selector(safeobjectAtIndexedSubscript:));

? ? ? ? method_exchangeImplementations(oldMutableObjectAtIndex1, newMutableObjectAtIndex1);

? ? });



}

//addobject

-(void)gp_addobjc:(id)object

{

? ? if(object !=nil) {

? ? ? ? [self gp_addobjc:object];//此時(shí)? 方法交換,所以不能在這寫addobject。若這樣的話,會(huì)造成死循環(huán)。

? ? }

}

//objectAtIndex

- (id)mutableObjectAtSafeIndex:(NSUInteger)index

{

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

? ? ? ? @try{

? ? ? ? ? ? return [self mutableObjectAtSafeIndex:index];

? ? ? ? }

? ? ? ? @catch(NSException *exception) {

? ? ? ? ? ? NSLog(@"exception: %@", exception.reason);

? ? ? ? ? ? returnnil;

? ? ? ? }


? ? }else{

? ? ? ? return [self mutableObjectAtSafeIndex:index];

? ? }

}

//aar[index]

- (instancetype)safeobjectAtIndexedSubscript:(NSUInteger)index{

? ? if(index > (self.count-1)) {// 數(shù)組越界

? ? ? ? returnnil;

? ? }else{// 沒有越界

? ? ? ? return [self safeobjectAtIndexedSubscript:index];

? ? }

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 轉(zhuǎn)至元數(shù)據(jù)結(jié)尾創(chuàng)建: 董瀟偉,最新修改于: 十二月 23, 2016 轉(zhuǎn)至元數(shù)據(jù)起始第一章:isa和Class一....
    40c0490e5268閱讀 2,055評(píng)論 0 9
  • 一、runtime之?dāng)?shù)據(jù)、字典越界 方法交換 Runtime解決數(shù)據(jù)越界及字典key或value為nil的情況,主...
    nenhall閱讀 569評(píng)論 0 1
  • 關(guān)于數(shù)組越界目前大概有兩種方式,一種是通過分類添加安全的索引方法,第二種就是Runtime實(shí)現(xiàn),第一種如果是個(gè)人開...
    dongke閱讀 595評(píng)論 0 0
  • 關(guān)于數(shù)組越界的系統(tǒng)崩潰的問題 小伙伴門經(jīng)常見到這個(gè)崩潰日志吧!! 能不能不不讓系統(tǒng)不直接崩潰,進(jìn)而影響用戶體驗(yàn)?。?..
    九林閱讀 389評(píng)論 1 1
  • ios開發(fā)中,不免會(huì)遇到數(shù)組越界的問題,而當(dāng)數(shù)組越界時(shí)往往會(huì)導(dǎo)致程序的崩潰,結(jié)局的方法之一就是在數(shù)組的分類中使用r...
    飛揚(yáng)的青春8780975閱讀 1,031評(píng)論 5 2

友情鏈接更多精彩內(nèi)容