nsarray was mutated while being enumerated
来源:互联网 发布:单片机51串口通信程序 编辑:程序博客网 时间:2024/06/08 07:09
在遍历数组过程中,找到要删除的元素并删除的时候会报错
尤其发生在使用array的block方法
/*Collection was mutated while being enumerated */// [commentList enumerateObjectsWithOptions:NSEnumerationConcurrent usingBlock:^(id obj, NSUInteger idx, BOOL *stop){// if ([[(NSDictionary *)obj objectForKey:@"id"] isEqualToString:dic[@"id"]]) {// [arr removeObjectIdenticalTo:dic];// }// }];正确的做法是将该数组拷贝一份,对新数组进行遍历,然后对原始数组中得元素进行删除操作
Using a for loop instead of enumerating is ok. But once you start deleting array elements, be careful if you're using threads. It's not enough to just decrement the counter as you may be deleting the wrong things. One of the correct ways is to create a copy of the array, iterate the copy and delete from the original.
见http://stackoverflow.com/questions/10157381/nsarray-was-mutated-while-being-enumerated
NSMutableArray *origins =[[NSMutableArray alloc]initWithArray:(NSArray *)[result objectForKey:@"data"]]; NSMutableArray *arr = [[NSMutableArray alloc]initWithArray:origins copyItems:YES]; if (arr==nil||[arr count]==0) { return ; } for (NSDictionary *dic in arr) { for (NSDictionary *dict in commentList) { if ([dic[@"id"] isEqualToString:dict[@"id"]]) { [origins removeObject:dic]; } }
0 0
- nsarray was mutated while being enumerated
- was mutated while being enumerated.
- was mutated while being enumerated
- was mutated while being enumerated.
- was mutated while being enumerated
- NSArray或者NSMutableArray常见错误was mutated while being enumerated
- 【Xcode】was mutated while being enumerated
- ios was mutated while being enumerated.
- Collection NSArrayM was mutated while being enumerated
- was mutated while being enumerated. 解决方法
- iOS - 解决was mutated while being enumerated
- was mutated while being enumerated.-terminate called throwing 
- 解决Collection <__NSArrayM: 0xb550c30> was mutated while being enumerated.-
- 解决Collection <__NSArrayM: 0xb550c30> was mutated while being enumerated.-
- 解决Collection <__NSArrayM: 0xb550c30> was mutated while being enumerated.-
- <__NSArrayM: 0xb550c30> was mutated while being enumerated.
- Collection <__NSArrayM: 0xxxxxxx> was mutated while being enumerated.
- Collection <__NSArrayM: 0xxxxxxx> was mutated while being enumerated.
- Paint事件搭配timer实现动态实时绘图(C#)
- IOCP编程小结(上)
- ym——安卓巴士总结了近百个Android优秀开源项
- 设计模式之代理模式
- 绘制有填充和无填充颜色的长方形和圆形(C#)
- nsarray was mutated while being enumerated
- 通过子界面(iframe中的)对父界面(框架界面)的弹框进行控制。
- Matlab代码加密/封装处理
- 16个很有用的在线工具
- ym——浅谈抽象与接口(含工厂、适配器、代理模式应用)(新)
- android通过JNI控制LED
- C#编译时出现“不安全代码只会在/unsafe编译的情况下出现”
- POJ 2887 分块链表 , vector 实现
- maven之生命周期