为什么我使用此NSMutableArray代码获取NSFastEnumerationMutationHandler?

时间:2013-06-07 00:58:03

标签: ios objective-c cocoa-touch nsmutablearray nsarray

有人可以帮助我理解我在这里做错了会导致这个堆栈跟踪:

1   libobjc.A.dylib                 0x3a8a897a objc_exception_throw + 26
2   CoreFoundation                  0x32b7fd80 __NSFastEnumerationMutationHandler + 124
3   CoreFoundation                  0x32adbcee -[NSArray containsObject:] + 134

以下是代码:

NSMutableArray *leftoverArray = [[NSMutableArray alloc] initWithArray:itemsArray];
for (NSDictionary *tempItem in tempItemsArray)
{
      if (![itemsArray containsObject:tempItem])
      {
           [itemsArray addObject:tempItem];
      }
      else
      {
           [leftoverArray removeObject:tempItem];
      }
}
for (NSDictionary *item in leftoverArray)
{
      [itemsArray removeObject:item];
}
[mainController.tblView reloadData];

tempItemsArray通过以下方式传递给此课程:

@property (nonatomic, strong) NSMutableArray *tempItemsArray;

我在我的应用中的其他地方有这个代码:

if (appDelegate.loading)
    appDelegate.tempItemsArray = itemsArray;
else
    appDelegate.itemsArray = itemsArray;
[tblView reloadData];

谢谢!

1 个答案:

答案 0 :(得分:2)

目前tempItemsArray和itemsArray是对同一个数组对象的引用。从技术上讲,您可以同时循环和修改同一个阵列。

尝试为tempItemsArray或itemsArray制作数组的副本:

if (appDelegate.loading)
    appDelegate.tempItemsArray = [NSMutableArray arrayWithArray:itemsArray];
else
    appDelegate.itemsArray = itemsArray;
[tblView reloadData];