對于剛剛接觸數(shù)組的新同學來說牢屋,應該最多遇到的就是類似于下面這樣的報錯了吧:
*** 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ù)組越界相關的問題令蛉,可能我們操作數(shù)組的時候不知道數(shù)組一共有多少個元素窑多,或者數(shù)組不是固定的碎赢,這樣在取值的時候就需要做判定:你要取的元素是否存在诵次,避免造成crash:
if (arr.count >5) {
NSLog(@"%@",[arr objectAtIndex:5]);
}
可是秒裕,這樣好煩人啊袱蚓,每次取元素還要做判定。不要擔心几蜻,利用Runtime(關于Runtime介紹喇潘,請自行Google),我們可以給NSArray做個方法替換梭稚,在自定義的objectAtIndex:方法里做下判定就好了颖低。
- (id)wj_objectAtIndex:(NSUInteger)index{
//判斷數(shù)組是否越界
if (index >= [self count]) {
return nil;
}
return [self wj_objectAtIndex:index];
}
這樣我們在使用時就可以放肆的調(diào)用了,如果越界了弧烤,就會返回nil忱屑,而不是崩潰了。
NSArray *arr = @[@"1",@"2"];
NSLog(@"%@",[arr objectAtIndex:5]);
不多說了,直接上代碼莺戒,Github地址:
https://github.com/zgsddzwj/WJSafeData
(利用Runtime更改NSArray伴嗡、NSDictionary,防止操作過程中因為越界引起的問題从铲,及一些便捷提取方式瘪校。 可以安全的對數(shù)組、字典中的數(shù)據(jù)進行操作食店,防止數(shù)組渣淤、字典越界)
本人QQ:297959735 郵箱:zgsddzwj@163.com,歡迎提意見吉嫩。