bug*** Terminating app due to uncaught exception ‘NSGenericException’, reason: ‘*** Collection <__NS
2017-07-06 16:10
543 查看
bug*** Terminating app due to uncaught exception ‘NSGenericException’, reason: ‘*** Collection <__NSArrayM: 0xb550c30> was mutated while being enumerated.’
当程序出现这个提示的时候,是因为你一边遍历数组,又同时修改这个数组里面的内容,导致崩溃,
找到符合的条件之后,暂停遍历,然后修改数组的内容
当程序出现这个提示的时候,是因为你一边遍历数组,又同时修改这个数组里面的内容,导致崩溃,
方法一:
网上的方法如下:这种方法就是在定义一个一模一样的数组,便利数组A然后操作数组BNSMutableArray * arrayTemp = xxx; NSArray * array = [NSArray arrayWithArray: arrayTemp]; for (NSDictionary * dic in array) { if (condition){ [arrayTemp removeObject:dic]; } }
方法二:
利用block来操作,根据查阅资料,发现block便利比for便利快20%左右,这个的原理是这样的:找到符合的条件之后,暂停遍历,然后修改数组的内容
NSMutableArray *tempArray = [[NSMutableArray alloc]initWithObjects:@"12",@"23",@"34",@"45",@"56", nil]; [tempArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { if ([obj isEqualToString:@"34"]) { *stop = YES; if (*stop == YES) { [tempArray replaceObjectAtIndex:idx withObject:@"3333333"]; } } if (*stop) { NSLog(@"array is %@",tempArray); } }];
相关文章推荐
- Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <__NSCFSet:
- xib:*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<NSObject 0x1
- Terminating app due to uncaught exception 'NSUnknownKeyException' reason: '[<ViewController 0x19862
- Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<YHTargetTableViewCell
- *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<DataModel 0x7fe44b
- Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<ViewController 0x19862
- Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UIViewController 0x687
- *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<DeviceStatusDetail
- Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <__NSArrayM:
- *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<ViewController 0x7
- Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString cont
- 出现Terminating app due to uncaught exception 'NSUnknownKeyException', reason: 错误的解决方法
- * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '* -[NSCFArr
- Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFArra
- *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not
- Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UITableView
- *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid
- ***Terminatingappduetouncaughtexception'NSInternalInconsistencyException', reason: 'CouldnotloadNIBi
- Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key
- iOS Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to