崩潰日志:*** Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <__NSArrayM: 0x61800024f7b0> was mutated while being enumerated.'
這是由于你一邊便利數組,又同時修改這個數組里面的內容,導致崩潰,
解決方法如下
方法一
NSMutableArray * tempArr = xxx;
NSArray * array = [NSArray arrayWithArray: tempArr];
for (NSDictionary * dic in array) {
if (OK){
[tempArr removeObject:dic];
}
}
方法一就是重新創建一個和你便利的數組一模一樣臨時數組,便利這個臨時數組,來修改你自己的所便利的原數組
方法二
NSMutableArray *tempArr = [[NSMutableArray alloc]initWithObjects:@"1",@"2",@"3",@"4",@"5", nil];
[tempArr enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
if ([obj isEqualToString:@"3"]) {
*stop = YES;
if (*stop == YES) {
[tempArr replaceObjectAtIndex:idx withObject:@"想改啥"];
}
}
if (*stop) {
NSLog(@"array is %@",tempArr);
}
}];
block 便利比for便利效率高,因為for便利,不管是否完成修改操作,還是要把所有的數組便利一遍,而block便利遇到滿足的條件就停止便利,執行修改操作,推薦使用block哦