Bug-iOS: Collection <__NSArrayM: 0x> was mutated while being enumerated.
2014-10-29 10:11
447 查看
//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, 是因为此时遍历已经结束, 结束之后对内容做修改是允许的 。
相关文章推荐
- 【Bug-iOS】Collection <__NSArrayM: 0x> was mutated while being enumerated.
- Bug-iOS: Collection <__NSArrayM: 0x> was mutated while being enumerated.
- 【学习ios之路:Objective-C错误】Collection <__NSArrayM: 0xb550c30> was mutated while being enumerated.
- iOS 报错Collection <__NSArrayM: 0x7fdf0b8154c0> was mutated while being enumerated.'
- iOS之解决崩溃Collection <__NSArrayM: 0xb550c30> was mutated while being enumerated.
- Collection <__NSArrayM: 0x> was mutated while being enumerated.
- iOS之解决崩溃Collection <__NSArrayM: 0xb550c30> was mutated while being enumerated.
- Collection <__NSArrayM: 0xb550c30> was mutated while being enumerated.-
- 【错误处理:】Collection <__NSArrayM: 0x12ec10e0> was mutated while being enumerated.
- *** Collection <__NSArrayM: 0x7f9fb3c48bc0> was mutated while being enumerated问题
- reason: ‘*** Collection <__NSArrayM: 0xb550c30> was mutated while being enumerated
- Collection <__NSArrayM: 0xxxxxxx> was mutated while being enumerated.
- 解决Collection <__NSArrayM: 0xb550c30> was mutated while being enumerated.-
- 解决 Crash:Collection <__NSArrayM: 0x61000024df50> was mutated while being enumerated
- 解决Collection <__NSArrayM: 0xb550c30> was mutated while being enumerated.-
- 解决Collection <__NSArrayM: 0xb550c30> was mutated while being enumerated.
- 解决Collection <__NSArrayM: 0xb550c30> was mutated while being enumerated.-
- Collection <__NSArrayM: 0xxxxxxx> was mutated while being enumerated.
- 解决Collection <__NSArrayM: 0xb550c30> was mutated while being enumerated.-
- 解决Collection <__NSArrayM: 0xb550c30> was mutated while being enumerated.-