当前位置: 代码迷 >> Iphone >> 关于NSGenericException-使用NSEnumerator,该如何解决
  详细解决方案

关于NSGenericException-使用NSEnumerator,该如何解决

热度:102   发布时间:2016-04-25 06:42:35.0
关于NSGenericException-使用NSEnumerator
[code=C/C++][/code]2012-05-18 17:17:52.990 Untitled[982:207] *** Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <__NSArrayM: 0x615c6a0> was mutated while being enumerated.(
  {
  img = "GunKing.png";
  name = "\U67aa\U738b\U4e4b\U738b";
  time = "01:30:20";
  },
  {
  img = "panda.png";
  name = "\U529f\U592b\U718a\U732b";
  time = "01:50:50";
  },
  {
  img = "pary.png";
  name = "\U5efa\U515a\U4f1f\U4e1a";
  time = "02:11:00";
  }
)'
*** Call stack at first throw:
(
0 CoreFoundation 0x00fa55a9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x010f9313 objc_exception_throw + 44
2 CoreFoundation 0x00fa5069 __NSFastEnumerationMutationHandler + 377
3 CoreFoundation 0x00f0b1eb -[__NSFastEnumerationEnumerator nextObject] + 75
4 Untitled 0x000022c3 -[RootviewAppDelegate getDictonary:] + 288
5 Untitled 0x0000a6ba -[DetailsViewController collectButtonPress] + 386
6 UIKit 0x002d14fd -[UIApplication sendAction:to:from:forEvent:] + 119
7 UIKit 0x00361799 -[UIControl sendAction:to:forEvent:] + 67
8 UIKit 0x00363c2b -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527
9 UIKit 0x003627d8 -[UIControl touchesEnded:withEvent:] + 458
10 UIKit 0x002f5ded -[UIWindow _sendTouchesForEvent:] + 567
11 UIKit 0x002d6c37 -[UIApplication sendEvent:] + 447
12 UIKit 0x002dbf2e _UIApplicationHandleEvent + 7576
13 GraphicsServices 0x018fd992 PurpleEventCallback + 1550
14 CoreFoundation 0x00f86944 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
15 CoreFoundation 0x00ee6cf7 __CFRunLoopDoSource1 + 215
16 CoreFoundation 0x00ee3f83 __CFRunLoopRun + 979
17 CoreFoundation 0x00ee3840 CFRunLoopRunSpecific + 208
18 CoreFoundation 0x00ee3761 CFRunLoopRunInMode + 97
19 GraphicsServices 0x018fc1c4 GSEventRunModal + 217
20 GraphicsServices 0x018fc289 GSEventRun + 115
21 UIKit 0x002dfc93 UIApplicationMain + 1160
22 Untitled 0x00002180 main + 102
23 Untitled 0x00002111 start + 53
)
terminate called after throwing an instance of 'NSException'



if ([m_array count]!=0)
{
NSEnumerator *enumerator = [m_array objectEnumerator];
NSDictionary *testDic;
while ((testDic = [enumerator nextObject]))
{
if ([testDic isEqualToDictionary:m_dictionary])
{
continue;
}
else
{
[m_array addObject:m_dictionary];
}

}
enumerator = nil;
}

else
{
[m_array addObject:m_dictionary];
}


网上查了一下,是因为使用 NSEnumerator 不当导致。 难道是因为数组加了数据导致变更 ,使得地址越界?

如果我想继续使用
NSEnumerator 达到一个遍历的效果,应该怎么修改。谢谢了

------解决方案--------------------
看了,不知道有没有误解你的意思。

我们在做遍历时,最好在遍历过程中不要对 遍历的原数据进行修改,如果要修改尽量用 for循环,
如果在遍历时 去修改 例如你的代码中 有 [m_array addObject:m_dictionary]; 这样的语句,可能就是导致异常的原因了。

  相关解决方案