“由于未捕获异常终止应用程序”在iOS上 [英] "Terminating app due to uncaught exception" on iOS
问题描述
我有一个 for
循环在我的代码。
当执行这个for循环继续,我的应用程序崩溃,并打印以下消息在控制台:
I have one for
loop in my code.
When execution of this for loop continues, my application crashes and prints the following message on the console:
Terminating app due to uncaught exception 'NSRangeException', reason: '-[NSMutableArray objectAtIndex:] index 2 beyond bounds [0 .. 1]' Call stack at first throw:
使用为
循环我试图填充一个 NSMutableArray
Using this for
loop I am trying to fill up a NSMutableArray
but this is not what is being done.
推荐答案
通常,当您尝试访问索引外部的元素时,会发生这种情况 NSArray
的边界。
Usually this happens when you try to access an element at an index outside of the bounds of the NSArray
.
所以说你有一个 NSArray
像这样:
So say you had an NSArray
like this:
NSArray *a = [NSArray arrayWithObjects:@"a", @"b", @"c", nil];
此代码将打印数组索引超出边界,因为边界为0 - 2:
This code would print "Array index out of bounds" because bounds are 0 - 2:
@try {
NSString *string = [a objectAtIndex:3];
} @catch(NSRangeException *e) {
NSLog(@"Array index out of bounds");
}
解决这个问题的最好方法是使用 fast enumeration :
The best way to solve this issue is to use fast enumeration:
for(id obj in array) {
//do something with obj
}
快速枚举使用枚举对象的 NSFastEnumeration
协议来处理所有的脏工作。
Fast enumeration uses the enumerable object's implementation of the NSFastEnumeration
protocol to handle all of the dirty work for you.
一个事情,通常会导致这个问题,即使使用快速枚举是如果你枚举一个可变结构例如 NSMutableArray
,并且在循环体中,使用 removeObject:
或其变体来变换结构,你会遇到这个异常迟早会因为结构的长度被缓存,因此它将继续下一次迭代,即使它超出边界。
One thing that will usually cause this problem even while using fast enumeration is if you are enumerating a mutable structure such as an NSMutableArray
and inside the body of the loop, you mutate the structure by using removeObject:
or its variants, you will run into this exception sooner than later because the length of the structure is cached and therefore it will carry on to the next iteration even if it goes out of bounds.
但是,采用快速枚举,你会抓住这个错误,而很快,因为内部的 __ NSFastEnumerationMutationHandler
将捕获它并抛出了以下异常:
But, using fast enumeration, you will catch this error rather quickly because the internal __NSFastEnumerationMutationHandler
will catch it and throw up the following exception:
2011-02-11 00:30:49.825 MutableNSFastEnumerationTest[10547:a0f] *** Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <NSCFArray: 0x10010c960> was mutated while being enumerated.<CFArray 0x10010c960 [0x7fff70c45ee0]>{type = mutable-small, count = 2, values = (
0 : <CFString 0x100001078 [0x7fff70c45ee0]>{contents = "b"}
1 : <CFString 0x100001058 [0x7fff70c45ee0]>{contents = "c"}
)}'
*** Call stack at first throw:
(
0 CoreFoundation 0x00007fff8621e7b4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x00007fff80daa0f3 objc_exception_throw + 45
2 CoreFoundation 0x00007fff862765bf __NSFastEnumerationMutationHandler + 303
3 MutableNSFastEnumerationTest 0x0000000100000de7 main + 295
4 MutableNSFastEnumerationTest 0x0000000100000cb8 start + 52
5 ??? 0x0000000000000001 0x0 + 1
)
terminate called after throwing an instance of 'NSException'
这篇关于“由于未捕获异常终止应用程序”在iOS上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!