【发布时间】:2014-02-15 07:07:15
【问题描述】:
我已经搜索过这个问题,我知道这是因为数组在枚举时被操作。在我的代码前面,我使用的是:
-(BOOL)busy
{
for(DataRequest* request in self.active)
if(!request.invisible)
return YES;
return NO;
}
和-(BOOL)busy 在从服务器加载数据时被非常频繁地调用。同样在我的代码中,我有一些行可以添加和删除self.active 中的对象。正因为如此,我得到了例外。然后我将代码更改为:
-(BOOL)busy
{
self.tempActive = self.active;
for(DataRequest* request in _tempActive)
if(!request.invisible)
return YES;
return NO;
}
但我仍然遇到同样的异常。我做错了什么,有什么想法吗?这是唯一使用self.tempActive 的代码。
我得到的例外是:
*** Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <__NSArrayM: 0x9ec8f60> was mutated while being enumerated.'
【问题讨论】:
-
有什么例外>?
-
self.tempActive = self.active 只是复制指针——因此你一直在引用同一个数组。我不确定您遇到了什么问题 - 但如果这与直接访问 self.active 有关,您应该复制它 self.tempActive = [self.active copy]。这样你就有了一个不同的对象,它是原始数组的副本。希望对您有所帮助。
标签: ios iphone nsmutablearray