iOS開發(fā)之?dāng)?shù)組越界(Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 5 beyond bounds [0 .. 1]')

對于剛剛接觸數(shù)組的新同學(xué)來說,應(yīng)該最多遇到的就是類似于下面這樣的報(bào)錯(cuò)了吧:

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 5 beyond bounds [0 .. 1]'
***First throw call stack:
(
    0   CoreFoundation                      0x000000010bbe5b0b __exceptionPreprocess + 171
    1   libobjc.A.dylib                     0x000000010b64a141 objc_exception_throw + 48
    2   CoreFoundation                      0x000000010bb2338b -[__NSArrayI objectAtIndex:] + 155
    3   WJSafeData                          0x000000010b076d1e -[NSArray(RuntimeSafe) wj_objectAtIndex:] + 62
    4   WJSafeData                          0x000000010b076177 -[ViewController viewDidLoad] + 183
    5   UIKit                               0x000000010c1ac01a -[UIViewController loadViewIfRequired] + 1235
    6   UIKit                               0x000000010c1ac45a -[UIViewController view] + 27
    7   UIKit                               0x000000010c07498a -[UIWindow addRootViewControllerViewIfPossible] + 65
    8   UIKit                               0x000000010c075070 -[UIWindow _setHidden:forced:] + 294
    9   UIKit                               0x000000010c087ebe -[UIWindow makeKeyAndVisible] + 42
    10  UIKit                               0x000000010c00137f -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 4346
    11  UIKit                               0x000000010c0075e4 -[UIApplication _runWithMainScene:transitionContext:completion:] + 1709
    12  UIKit                               0x000000010c0047f3 -[UIApplication workspaceDidEndTransaction:] + 182
    13  FrontBoardServices                  0x000000010f1bf5f6 __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 24
    14  FrontBoardServices                  0x000000010f1bf46d -[FBSSerialQueue _performNext] + 186
    15  FrontBoardServices                  0x000000010f1bf7f6 -[FBSSerialQueue _performNextFromRunLoopSource] + 45
    16  CoreFoundation                      0x000000010bb8bc01 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    17  CoreFoundation                      0x000000010bb710cf __CFRunLoopDoSources0 + 527
    18  CoreFoundation                      0x000000010bb705ff __CFRunLoopRun + 911
    19  CoreFoundation                      0x000000010bb70016 CFRunLoopRunSpecific + 406
    20  UIKit                               0x000000010c00308f -[UIApplication _run] + 468
    21  UIKit                               0x000000010c009134 UIApplicationMain + 159
    22  WJSafeData                          0x000000010b07828f main + 111
    23  libdyld.dylib                       0x000000010ea4f65d start + 1
    24  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

類似的還有:

*** Terminating app due to uncaught exception 'NSRangeException', reason:'
*** -[__NSSingleObjectArrayI objectAtIndex:]: index 5 beyond bounds [0 .. 0]'
 *** Terminating app due to uncaught exception 'NSRangeException', reason: 
'*** -[__NSArrayM insertObject:atIndex:]: index 5 beyond bounds [0 .. 2]'
*** Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <__NSArrayM: 0x608000057f40> was mutated while being enumerated.'

以上都是數(shù)組越界相關(guān)的問題,可能我們操作數(shù)組的時(shí)候不知道數(shù)組一共有多少個(gè)元素,或者數(shù)組不是固定的,這樣在取值的時(shí)候就需要做判定:你要取的元素是否存在,避免造成crash:

if (arr.count >5) {
        NSLog(@"%@",[arr objectAtIndex:5]);
    }

可是,這樣好煩人啊,每次取元素還要做判定。不要擔(dān)心,利用Runtime(關(guān)于Runtime介紹,請自行Google),我們可以給NSArray做個(gè)方法替換,在自定義的objectAtIndex:方法里做下判定就好了。

- (id)wj_objectAtIndex:(NSUInteger)index{
    //判斷數(shù)組是否越界
    if (index >= [self count]) {
        return nil;
    }
    return [self wj_objectAtIndex:index];
}

這樣我們在使用時(shí)就可以放肆的調(diào)用了,如果越界了,就會返回nil,而不是崩潰了。

NSArray *arr = @[@"1",@"2"];
NSLog(@"%@",[arr objectAtIndex:5]);

不多說了,直接上代碼,Github地址:
https://github.com/zgsddzwj/WJSafeData
(利用Runtime更改NSArray、NSDictionary,防止操作過程中因?yàn)樵浇缫鸬膯栴},及一些便捷提取方式。 可以安全的對數(shù)組、字典中的數(shù)據(jù)進(jìn)行操作,防止數(shù)組、字典越界)

本人QQ:297959735 郵箱:zgsddzwj@163.com,歡迎提意見。

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

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

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