应用崩溃并出现异常:*** 集合在枚举时发生了变化



应用程序崩溃并出现异常:

集合在被枚举时发生了变异。2016-01-25 08:55:30.606 Mink Chatter[416:70120] 由于未捕获的异常"NSGenericException"而终止应用,原因:"*** 集合在枚举时发生突变。第一个抛出调用堆栈:(0x1821c5900 0x181833f80 0x1821c5334 0x186ead9f0 0x186ead7d8 0x186ead6d8 0x1001a6834 0x10015ddc0 0x100905bf0 0x100905bb0 0x100914e10 0x1009144d8 0x181e2d470 0x181e2d020)libc++abi.dylib:以 NSException 类型的未捕获异常终止

法典:

self.refreshIndicators = [[NSMutableArray alloc] init];
for (NSInteger i = 0; i < 3; i++) {
    UIView *indicator = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.height, self.frame.size.height)];
    float delay = 0.4 * i;
    [UIView animateWithDuration:1.2 delay:delay options:UIViewAnimationOptionCurveEaseIn|UIViewAnimationOptionRepeat animations:^{
    } completion:^(BOOL finished) {
    }];
    [self addSubview:indicator];
    [self.refreshIndicators addObject:indicator];
} 

当您使用 for-loop(for (id _obj in object))枚举某个对象(例如 NSMutableArray 或 NSMutableSet)并在该循环中删除或向同一对象添加某些对象时,会出现此问题。这是不可能的,并导致异常。

试试这个:将代码保持在下面的循环中。

@synchronized(self)
  {
       // your code goes here 
  }

希望这有帮助。

最新更新