崩潰日志:*** Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <__NSArrayM: 0x61800024f7b0> was mutated while being enumerated.'
這是由于你一邊便利數(shù)組,又同時(shí)修改這個(gè)數(shù)組里面的內(nèi)容,導(dǎo)致崩潰,
解決方法如下
方法一
NSMutableArray * tempArr = xxx;
NSArray * array = [NSArray arrayWithArray: tempArr];
for (NSDictionary * dic in array) {
if (OK){
[tempArr removeObject:dic];
}
}
方法一就是重新創(chuàng)建一個(gè)和你便利的數(shù)組一模一樣臨時(shí)數(shù)組,便利這個(gè)臨時(shí)數(shù)組,來修改你自己的所便利的原數(shù)組
方法二
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便利效率高,因?yàn)閒or便利,不管是否完成修改操作,還是要把所有的數(shù)組便利一遍,而block便利遇到滿足的條件就停止便利,執(zhí)行修改操作,推薦使用block哦