美文网首页
<__NSArrayM:XXXXXX> was mutated

<__NSArrayM:XXXXXX> was mutated

作者: 牛程程 | 来源:发表于2018-06-26 22:53 被阅读0次

    这样的代码运行起来会出现崩溃

    //targetButton
    for (UIButton * button in self.selectedBtnArr)
     {
        if (targetButton.tag ==  button.tag)
       {
            [self.selectedBtnArr removeObject:button];
        }
    }
    

    在对可变数据类型如字典、数组,进行快速遍历的时候,是不可以对其增、删操作。否则就会引起“<__NSArrayM:XXXXXX> was mutated while being enumerated.”的崩溃。

    官网对快速排序的说明(https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSEnumerator_Class/index.html](https://link.jianshu.com?t=https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSEnumerator_Class/index.html)
    )。重点为NOTE的内容:

    It is not safe to modify a mutable collection while enumerating through it. Some enumerators may currently allow enumeration of a collection that is modified, but this behavior is not guaranteed to be supported in the future.

    解决方案:

    //targetButton
    for (UIButton * button in self.selectedBtnArr)
     {
        if (targetButton.tag ==  button.tag)
       {
            [self.selectedBtnArr removeObject:button];
            break;
        }
    }
    

    相关文章

      网友评论

          本文标题:<__NSArrayM:XXXXXX> was mutated

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