Collection <__NSArrayM: 0x610000244590> was mutated while being enumerated.
~ 控制台出现这个错误的时候是因为,在一边遍历数组一边去修改数组,就像这样:
for (OrderModel *model in self.modelAryl) {
if ([model.delivery_type isEqualToString:@"1"]) {
[self.modelAryl removeObject:model];
}
}
for (OrderDetailModel *model in self.orderDetailModelAryl) {
if ([model.delivery_type isEqualToString:@"1"]) {
[self.orderDetailModelAryl removeObject:model];
}
}
~ 可以通过增加中间变量来解决:
@property (nonatomic, strong) NSMutableArray *modelAry;
@property (nonatomic, strong) NSMutableArray *orderDetailModelAry;
@property (nonatomic, strong) NSMutableArray *modelAryl; //中间变量
@property (nonatomic, strong) NSMutableArray *orderDetailModelAryl; //中间变量
~ 注意中间变量赋值的时候不能写成:
self.modelAryl = self.modelAry;
这样的话还是会有错误,因为内存地址其实是一样的,同样是会改变原先数组的,所以实质上都是遍历并修改了。
~ 应该这样写:
self.modelAryl = [NSMutableArray arrayWithArray:self.modelAry];
另一个数组采用同样方式,问题解决。