美文网首页
was mutated while being enumerat

was mutated while being enumerat

作者: 纯阳子_ | 来源:发表于2017-04-20 14:34 被阅读30次
写了一段这样的代码:

for (ShopCartModel *model in weakSelf.dataArray) {
      for (NSString *id_ in ids) {
               if ([[NSString stringWithFormat:@"%@",model.id_] isEqualToString:[NSString stringWithFormat:@"%@",id_]]) {
                   [weakSelf.dataArray removeObject:model];
                }
       }
}

抛出异常: was mutated while being enumerated.'

解决方法(对数组进行备份):

NSArray *array = [NSArray arrayWithArray:weakSelf.dataArray];

for (ShopCartModel *model in array) {
     for (NSString *id_ in ids) {
          if ([[NSString stringWithFormat:@"%@",model.id_] isEqualToString:[NSString stringWithFormat:@"%@",id_]]) {
                    [weakSelf.dataArray removeObject:model];
            }
      }
}

相关文章

网友评论

      本文标题:was mutated while being enumerat

      本文链接:https://www.haomeiwen.com/subject/hpllzttx.html