美文网首页
iOS 报错<__nsarraym:xxxxxx> was mu

iOS 报错<__nsarraym:xxxxxx> was mu

作者: eden_lx | 来源:发表于2020-04-11 20:56 被阅读0次

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

//targetButton

for (UIButton * button in self.selectedBtnArr)

{

    if (targetButton.tag ==  button.tag)

    {

        [self.selectedBtnArr removeObject:button];

    }

}

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

官网对快速排序的说明官方文档链接)。重点为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.

解决方案:

for (UIButton * button in self.selectedBtnArr)

{

    if (targetButton.tag ==  button.tag)

    {

        [self.selectedBtnArr removeObject:button];

        break;

    }

}

相关文章

网友评论

      本文标题:iOS 报错<__nsarraym:xxxxxx> was mu

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