崩溃提示:Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <CALayerArray: 0x14df0bd0> was mutated while being enumerated.'

网上找了下,原因是因为一边遍历数组,又同时修改这个数组里面的内容,导致崩溃。

for in 循环中的便利内容不能被改变, 是因为如果改变其便利的内容会少一个, 而系统是不会允许这个发生的所以就会crash...但是当改变最后一个的内容时, 就不会crash, 是因为此时遍历已经结束, 结束之后对内容做修改是允许的。

有两种解决方法,第一种方法就是在定义一个一模一样的数组,便利数组A然后操作数组B; 第二种方法是找到符合的条件之后,暂停遍历,然后修改数组的内容。

解决方法详细代码分别如下:

NSMutableArray *mutArray = [[NSMutableArray alloc] initWithObjects:@"1",@"2",@"3",@"4",nil];

NSArray *array = [NSArray arrayWithArray: mutArray];  

    for (NSString *str in array) { 

        if ([str isEqualToString:@"3"]){     

            [mutArray removeObject: str];

        }       

    }
    NSMutableArray *mutArray = [[NSMutableArray alloc]initWithObjects:@"1",@"2",@"3",@"4",nil];

    [mutArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

        if ([obj isEqualToString:@"3"]) {

           *stop = YES;

            if (*stop == YES) {

                [mutArray removeObject:obj];

            }
} if (*stop) { NSLog(@"array is %@",mutArray); } }];

bug:*** Collection <__NSArrayM: 0x1c444d440> was mutated while being enumerated.的更多相关文章

  1. 解决Collection <__NSArrayM: 0xb550c30> was mutated while being enumerated.-

    bug: 今天做项目的时候遇到了这样一个崩溃信息: 解决Collection <__NSArrayM: 0xb550c30> was mutated while being enumera ...

  2. *** Collection <__NSArrayM: 0x600000647380> was mutated while being enumerated.

    *** Collection <__NSArrayM: 0x600000647380> was mutated while being enumerated.

  3. iOS之解决崩溃Collection <__NSArrayM: 0xb550c30> was mutated while being enumerated.

    崩溃提示:Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <CAL ...

  4. 解决Collection <__NSArrayM: 0x7f8168f7a750> was mutated while being enumerated.'

    当程序出现这个提示的时候,是因为你一边便利数组,又同时修改这个数组里面的内容,导致崩溃,网上的方法如下: NSMutableArray * arrayTemp = xxx; NSArray * arr ...

  5. reason: '*** Collection <__NSCFArray: 0x7ffa43528f70> was mutated while being enumerated.'

    一,错误分析 1.崩溃代码如下: //遍历当前数组,判断是否有相同的元素 for (NSString *str in self.searchHistoryArrM) { if ([str isEqua ...

  6. 【转】was mutated while being enumerated 你是不是以为你真的懂For...in... ??

    原文网址:http://www.jianshu.com/p/ad80d9443a92 支持原创,如需转载, 请注明出处你是不是以为你真的懂For...in... ??哈哈哈哈, 我也碰到了这个报错 . ...

  7. 异常Crash之 NSGenericException,NSArray was mutated while being enumerated

    *** Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <__NS ...

  8. Objective-C Collection was mutated while being enumerated crash

    Collection was mutated while being enumerated

  9. [Bug]枚举数组,并找到某些元素删除

    lldb报错:Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <_ ...

随机推荐

  1. Go指南练习_切片

    源地址 https://tour.go-zh.org/moretypes/18 一.练习题描述 实现 Pic.它应当返回一个长度为 dy 的切片,其中每个元素是一个长度为 dx,元素类型为 uint8 ...

  2. Erlang的gen_server的terminate()/2未执行

    官方资料参考: Module:terminate(Reason, State) Types: Reason = normal | shutdown | {shutdown,term()} | term ...

  3. expect脚本同步文件 expect脚本指定host和要同步的文件 构建文件分发系统 批量远程执行命令

    自动同步文件 #!/usr/bin/expect set " spawn rsync -av root@.txt /tmp/ expect { "yes/no" { se ...

  4. spring boot mybatis 整合教程

    本项目使用的环境: 开发工具:Intellij IDEA 2017.1.3 springboot: 1.5.6 jdk:1.8.0_161 maven:3.3.9 额外功能 PageHelper 分页 ...

  5. SpringBoot Logback配置,SpringBoot日志配置

    SpringBoot Logback配置,SpringBoot日志配置  SpringBoot springProfile属性配置 ================================ © ...

  6. Linux中 SonarQube代码质量管理平台安装

    SonarQube是管理代码质量一个开源平台,可以快速的定位代码中潜在的或者明显的错误. SonarQube安装 1.环境准备 (1)sonarQube 下载地址https://www.sonarqu ...

  7. 9.24 Django Form组件

    2018-9-23 20:10:04 这两天优化了自己图书管理系统 github 连接:https://github.com/TrueNewBee/pythonDemo 顺便整理了博客,写了好多总结, ...

  8. go 的文件处理

    准备一个文件 imooc.txt hello world! 一.使用 io/ioutil 包 定义一个 check 函数 func check(err error) { if err != nil { ...

  9. .NET Core开发日志——从搭建开发环境开始

    .NET Core自2016年推出1.0版本开始,到目前已是2.1版本,在其roadmap计划里明年更会推出3.0版本,发展不可不谓之迅捷.不少公司在经过一个谨慎的观望期后,也逐步开始将系统升级至最新 ...

  10. css学习_css三大特性

    css三大特性 1.层叠性(就近原则) 2.继承性(和文字有关的会继承) 3.优先级   (权重问题) 权重:0,0,0,0 0001 ---标签选择器(注意:即使有20个标签选择器也不会比一个伪类选 ...