遍历数组修改数据导致的错误
2016-05-27 11:32
288 查看
错误提示:
Terminating app due to uncaught exception ‘NSGenericException’, reason: ‘*** Collection <__NSArrayM: 0xb550c30>
was mutated while being enumerated.’
原因:由于遍历数组时候 修改了数组中的数据 导致错误。
解决办法:
定义一个数组B,并把要遍历的数组A的值赋给他 。
然后遍历数组B 要修改数据的A就可以修改数据了。
NSArray *arr = content[@"workface"];
for (NSDictionary *dictin arr) {
if ([dict[@"workfaceid"]isEqual:_faceInfo[@"workfaceid"]])
{
//
先移除后添加
[content[@"workface"]removeObject:dict];
[content[@"workface"]addObject:_faceInfo];
}
}
另一种快捷 的方法是
[self.viceMoresels
enumerateObjectsUsingBlock:^(id
_Nonnull obj, NSUInteger idx,
BOOL * _Nonnull stop) {
if ([obj
isEqualToString:[@(sender.tag-10001)
stringValue]]) {
*stop = YES;
if (*stop==YES) {
[self.viceMoresels
removeObjectAtIndex:idx];
}
}
}];
这种方法 遍历速度更快 。
Terminating app due to uncaught exception ‘NSGenericException’, reason: ‘*** Collection <__NSArrayM: 0xb550c30>
was mutated while being enumerated.’
原因:由于遍历数组时候 修改了数组中的数据 导致错误。
解决办法:
定义一个数组B,并把要遍历的数组A的值赋给他 。
然后遍历数组B 要修改数据的A就可以修改数据了。
NSArray *arr = content[@"workface"];
for (NSDictionary *dictin arr) {
if ([dict[@"workfaceid"]isEqual:_faceInfo[@"workfaceid"]])
{
//
先移除后添加
[content[@"workface"]removeObject:dict];
[content[@"workface"]addObject:_faceInfo];
}
}
另一种快捷 的方法是
[self.viceMoresels
enumerateObjectsUsingBlock:^(id
_Nonnull obj, NSUInteger idx,
BOOL * _Nonnull stop) {
if ([obj
isEqualToString:[@(sender.tag-10001)
stringValue]]) {
*stop = YES;
if (*stop==YES) {
[self.viceMoresels
removeObjectAtIndex:idx];
}
}
}];
这种方法 遍历速度更快 。
相关文章推荐
- ehcache 分布式集群同步数据实例
- eclipse hana xs 开发环境搭建
- redis主从模式
- 0基础产品经理之我见(笔记)
- HDOJ 1789 Doing Homework again
- 论Json解析途径及XML的对比
- c/s架构nginx+php-fpm通信原理
- 移位运算符
- FPGA verilog做基于模板的图像处理的方法
- Linux中fork()函数
- 门禁控制器编程方法
- Activity工作流学习要点
- CDH管理界面查看各框架的版本(hive为例)
- 项目中用到listview总结(一)
- 简单易用的数组排序(升序、降序)
- JAVA堆内存设置管理
- Java用freemarker导出word
- ios 对FMDB 简单封装
- 4章
- Fourinone分布式部署配置文档