iphone - NSMutableArray 替换对象

标签 iphone objective-c

我尝试在数组中查找对象,如果成功,我需要将数组中的对象替换为新对象

 for (id existingSig in allSignature)
   if ([[existingSig objectForKey:@"SignatureName"] isEqualToString:[item objectForKey:@"name"]])
    {    
      [allSignature removeObject:existingSig];
      [allSignature addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"1", @"SignatureIsRich", [item objectForKey:@"name"], @"SignatureName", generatedID, @"SignatureUniqueId", nil]];
     }

我遇到错误“NSCFArray: 0x100551f10> 在枚举时发生突变”

最佳答案

正如其他人所说,当 MutableArray 被枚举时,你不能改变它。您可以通过在循环后使用两个数组来处理它,其中包含要删除的内容和要添加的内容。

NSMutableArray *remove = [NSMutableArray array];
NSMutableArray *add = [NSMutableArray array];
for (id existingSig in allSignature){
   if ([[existingSig objectForKey:@"SignatureName"] isEqualToString:[item objectForKey:@"name"]])
    {    
      // Add to the array of objects to be removed
      [remove addObject:existingSig];
      // Add to the array of objects to be added
      [add addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"1", @"SignatureIsRich", [item objectForKey:@"name"], @"SignatureName", generatedID, @"SignatureUniqueId", nil]];
     }
}
[allSignature removeObjectsInArray:remove]; // Remove objects
[allSignature addObjectsFromArray:add]; // Add new objects

关于iphone - NSMutableArray 替换对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10457527/

相关文章:

iphone - 滚动性能和UIImage绘制

iphone - 如何在 iOS 中将 webview 的内容垂直和水平居中?

ios - 32 位的应用程序可以在 64 位的 iPhone 5S 上运行吗?

iphone - 在 UIPopover 中显示 UIDatePicker

ios - -(Class)class 是什么意思?

iphone - 使用 bit.ly 缩短网址

iphone - 应用程序在i0S 6中崩溃,但在iOS 5中没有崩溃

objective-c - 使用 SearchBar 时出现 NSUnknownKeyException。此类不符合键名的键值编码

iphone - NSMutableArray 检查对象是否已经存在

ios - 变量的类型不完整 'QPrinter'