崩溃提示: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. .NET Memory Allocation Profiling with Visual Studio 2012

    .NET Memory Allocation Profiling with Visual Studio 2012 This post was written by Stephen Toub, a fr ...

  2. Fedora 21 安装 Budgie Desktop

    最新文章:Virson's Blog Budgie Desktop 是一款自由开源桌面,是 Evolve OS 的默认桌面,Evolve OS 是一款 OpenSUSE 的衍生系统.Budgie De ...

  3. Cocos2d-x 3.0 纹理

    1.纹理控制. Sprite *pSprite = Sprite::create("background.png"); TexParams params = {GL_NEAREST ...

  4. Numpy 定义矩阵的方法

    import numpy as np #https://www.cnblogs.com/xzcfightingup/p/7598293.html a = np.zeros((2,3),dtype=in ...

  5. QT QQuickView嵌入到QT MDI中

    在学习QT的过程中发现有一个特别炫酷的行星例子“planets”,有两种实现版本: 一种是基于Qt 3D QML(planets-qml),另一种则是基于Quick和强大的Three.js(plane ...

  6. linux 设置中文版man手册

    作为CentOS 新手,看懂英文man固然重要,不过配置好中文man也可以让自己更快速地学习!1. 下载中文man包源码的网址:https://src.fedoraproject.org/repo/p ...

  7. [IR] Suffix Trees and Suffix Arrays

    前缀树 匹配前缀字符串是不言自明的道理. 1. 字符串的快速检索 2. 最长公共前缀(LCP) 等等 树的压缩 后缀树 Let s=abab, a suffix tree of s is a comp ...

  8. Win10系统安装过程小记

    1.网上下载ghost系统http://win10.jysmac.cn/win1064.html 2.使用系统自带的激活工具激活 3.到windows官网下载更新工具更新系统,重新安装https:// ...

  9. [Linux] 设置系统时区

    1. 检查当前时区 以 root 身份登录. # date Fri Sep :: UTC 其中 UTC 是指当前使用的时间系统为世界标准时间,也称世界协调时间.英文名称为 Coordinated Un ...

  10. [原]Jenkins(十三)---jenkins用户权限管理

    * 版权声明:本博客欢迎转发,但请保留原作者信息! http://www.cnblogs.com/horizonli/p/5337874.html 两种策略的比较