Bug-iOS: Collection <__NSArrayM: 0x> was mutated while being enumerated.
//init a NSArray NSMutableArray *arr2 = [NSMutableArray array]; for (int i=0; i<10; ++i) { NSMutableDictionary *d = [NSMutableDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"%d", i], @"a", nil]; [arr2 addObject:d]; } //Wrong for (NSMutableDictionary *s in arr2) { if ([s[@"a"] isEqualToString:@"3"]) { [arr2 removeObject:s]; //while here is safe for s[@"a"] = @"eeee"; } } //Safe 1. [arr2 enumerateObjectsUsingBlock:^(NSMutableDictionary *obj, NSUInteger idx, BOOL *stop) { if ([obj[@"a"] isEqualToString:@"3"]) { *stop = YES; [arr2 removeObject:obj]; // or obj[@"a"] = @"4ewewewe"; } }]; //Safe 2. NSArray *tmp = [NSArray arrayWithArray:arr2]; for (NSMutableDictionary *s in tmp) { if ([s[@"a"] isEqualToString:@"3"]) { s[@"a"] = @"eeee"; [arr2 removeObject:s]; } }
PS: 搜一句补充的话
//forin 循环中的便利内容不能被改变, 是因为如果改变其便利的内容会少一个, 而系统是不会允许这个发生的所以就会crash...但是当改变最后一个的内容时, 就不会crash, 是因为此时遍历已经结束, 结束之后对内容做修改是允许的 。
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。