was mutated while being enumerated
来源:互联网 发布:84cs域名升级访问升级 编辑:程序博客网 时间:2024/05/18 00:25
在oc中会出现这种情况,当时不知道怎么回事,就一直查代码,因为以前写的就没有问题,为什么这次就出现问题了。
- (void)deleteContactsForName:(NSString *)name{ for (NSString * groupName in _contactDic) { NSMutableArray * oneGroup =[_contactDic objectForKey:groupName]; for (Contact * c in oneGroup) { if ([[c name ] isEqualToString:@"陈达"]) { [oneGroup removeObject:c]; } } }}
其错误的显示如下
主要是因为在遍历数组的时候,又改变了数组。另一方面是因为forin叫做快速遍历,问什么快速呢?因为forin开启了多线程,在多线程下,不能同时改变数组,又同时遍历数组。那么单线程呢,单线程是可以的,所以在这种情况下,没有没得方法,只能用普通的for循环了。当然你要是学过多线程的话,可以用线程锁来防止这种情况。这次,就不演示使用线程锁了,只演示一下普通for循环来解决此方法。
直接上代码
// 根据姓名删除联系人- (void)deleteContactsForName:(NSString *)name{ for (NSString * string in _contactDic) { NSMutableArray * oneGroup =[_contactDic objectForKey:string]; int n = (int)[oneGroup count]; for (int i = 0; i < n; i++) { if ([[oneGroup[i] name]isEqualToString:name]) { [oneGroup removeObjectAtIndex:i]; i--; n--; } } }}
0 0
- was mutated while being enumerated.
- was mutated while being enumerated
- was mutated while being enumerated.
- was mutated while being enumerated
- 【Xcode】was mutated while being enumerated
- nsarray 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.-
- NSArray或者NSMutableArray常见错误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.
- 最少步数 SDUT 3080
- PHP 命名空间
- 网络信息安全攻防实验室之基础关
- Java中abstract和final、static
- Code Forces 525C Ilya and Sticks
- was mutated while being enumerated
- C++(32)
- UI中简单的猜数字游戏
- 【python网络编程】新浪爬虫:关键词搜索爬取微博数据
- js同一页面iframe调取另一iframe的url
- 进程和线程的差别
- 【干货】Android studio教程与问题汇总
- Java网络编程
- Caffe学习:Layers