<__NSArrayM:XXXXXX> was mutated

2018-06-26  本文已影响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;
    }
}
上一篇下一篇

猜你喜欢

热点阅读