NSMutableArray替换对象 [英] NSMutableArray replace object

查看:163
本文介绍了NSMutableArray替换对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I try to find object in my array and if success I need to replace object from my array to new object

 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>在被枚举时被突变'

I have error 'NSCFArray: 0x100551f10> was mutated while being enumerated'

推荐答案

正如其他人所说,在枚举时你不能改变MutableArray。你可以通过两个数组去除循环后要添加的内容以及循环后添加的内容来处理它。

As others have said, you cannot mutate a MutableArray while it is being Enumerated. You could handle it by having two arrays of what to remove and what to add after the loop.

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

这篇关于NSMutableArray替换对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆