這樣的代碼運(yùn)行起來會(huì)出現(xiàn)崩潰
//targetButton
for (UIButton * button in self.selectedBtnArr)
{
if (targetButton.tag == button.tag)
{
[self.selectedBtnArr removeObject:button];
}
}
在對可變數(shù)據(jù)類型如字典、數(shù)組,進(jìn)行快速遍歷的時(shí)候,是不可以對其增、刪操作。否則就會(huì)引起“<__NSArrayM:XXXXXX> was mutated while being enumerated.”的崩潰。
官網(wǎng)對快速排序的說明(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)
)。重點(diǎn)為NOTE的內(nèi)容:
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;
}
}