对于刚刚接触数组的新同学来说,应该最多遇到的就是类似于下面这样的报错了吧:
*** 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.'
以上都是数组越界相关的问题,可能我们操作数组的时候不知道数组一共有多少个元素,或者数组不是固定的,这样在取值的时候就需要做判定:你要取的元素是否存在,避免造成crash:
if (arr.count >5) {
NSLog(@"%@",[arr objectAtIndex:5]);
}
可是,这样好烦人啊,每次取元素还要做判定。不要担心,利用Runtime(关于Runtime介绍,请自行Google),我们可以给NSArray做个方法替换,在自定义的objectAtIndex:方法里做下判定就好了。
- (id)wj_objectAtIndex:(NSUInteger)index{
//判断数组是否越界
if (index >= [self count]) {
return nil;
}
return [self wj_objectAtIndex:index];
}
这样我们在使用时就可以放肆的调用了,如果越界了,就会返回nil,而不是崩溃了。
NSArray *arr = @[@"1",@"2"];
NSLog(@"%@",[arr objectAtIndex:5]);
不多说了,直接上代码,Github地址:
https://github.com/zgsddzwj/WJSafeData
(利用Runtime更改NSArray、NSDictionary,防止操作过程中因为越界引起的问题,及一些便捷提取方式。 可以安全的对数组、字典中的数据进行操作,防止数组、字典越界)
本人QQ:297959735 邮箱:zgsddzwj@163.com,欢迎提意见。