简体   繁体   English

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

[英]Why am I getting a NSFastEnumerationMutationHandler with this NSMutableArray code?

Can someone help me understand what I am doing wrong here that would caused this stack trace: 有人可以帮助我理解我在这里做错了会导致这个堆栈跟踪:

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

Here is the code: 这是代码:

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 is passed in to this class via: tempItemsArray通过tempItemsArray方式传递给此类:

@property (nonatomic, strong) NSMutableArray *tempItemsArray;

I do have this code elsewhere in my app: 我在我的应用程序的其他地方有这个代码:

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

Thanks! 谢谢!

Currently tempItemsArray and itemsArray are a reference to the same array object. 目前tempItemsArray和itemsArray是对同一个数组对象的引用。 You are technically looping and modifying the same array at the same time. 从技术上讲,您可以同时循环和修改同一个阵列。

Try to make a copy of the array for the tempItemsArray or itemsArray: 尝试为tempItemsArray或itemsArray制作数组的副本:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么在此简单代码上出现“错误访问”异常? - Why Am I getting Bad Access exception on this simple code? 为什么我的ibtool失败,退出代码为255? - Why am I getting ibtool failed with exit code 255? 为什么我会收到NSUnkownKeyException? - Why am I getting NSUnkownKeyException? 为什么我可以得到405个时间估计代码,却可以得到200个产品类型代码 - Why am I getting 405 code for Time Estimates while I can get 200 code for Product Types 当我尝试从情节提要中选择时,为什么在这一行代码中出现段错误? - Why am I getting a seg fault in this line of code when I try to segue from storyboard? 为什么我在尝试扫描二维码时会在 Expo 中收到此错误? - Why am I getting this error in Expo when trying to scan QR code? RevenueCat - 为什么我得到:Error Domain=SKErrorDomain Code=2“无法连接到 iTunes Store” - RevenueCat - Why am I getting: Error Domain=SKErrorDomain Code=2 "Cannot connect to iTunes Store" 为什么在此代码中得到“只能将实例方法声明为@IBAction”? - Why am I getting “Only instance methods can be declared @IBAction” in this code? 为什么我在cordova中添加ios时“命令失败并退出代码ENOENT”? - why i am getting “command failed with exit code ENOENT” when adding ios in cordova? 为什么我从Xcode收到此警告? iOS代码似乎很好 - Why am I getting this warning from Xcode? The iOS code seems fine
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM