尝试将字典添加到nsMutableArray时,获取“无法识别的选择器”

时间:2014-11-27 21:19:07

标签: ios for-loop nsmutablearray nsdictionary nsmutabledictionary

我有一个NSMutableArray打印类似的东西:

<__NSArrayM 0x7a64f450>(
{
    user = "Mark";
    age = 21;
},
{
    user = "Bill";
    age = 43;
}...

我在这里循环添加另一个键:

 NSMutableDictionary *dic = [NSMutableDictionary dictionary];
 [dic setValue:carString forKey:@"car"];
  for(int i = 0; i < [final count]; i++) {
   NSLog(@"final is %@",[final objectAtIndex:i]);
 [[final objectAtIndex:i] addEntriesFromDictionary:car];

当我打印决赛时#i;得到:

final is {
        user = "Mark";
        age = 21;
}

但是当我尝试添加条目时,我收到错误: - [__ NSDictionaryI addEntriesFromDictionary:]:无法识别的选择器发送到实例0x7b803330&#39;

如何添加字典以便最终:     最后是{             user =&#34; Mark&#34 ;;             年龄= 21岁;             car =&#34;福特&#34 ;;     }

我尝试过使用NSDictionary而不是NSMutableDictionary。我不确定将NSMutableArray更改为NSMutableDictionary是否可行或是否可行(尚未在SO上找到解决方案)。

1 个答案:

答案 0 :(得分:0)

存储在final数组中的字典是不可变的。您需要使用包含合并对象的可变字典替换它们 -

NSMutableDictionary *dic = [NSMutableDictionary dictionary];
[dic setValue:carString forKey:@"car"];
for(int i = 0; i < [final count]; i++) {
   NSMutableDictionary *mutableDict=[final[i] mutableCopy];
   [mutableDict addEntriesFromDictionary:car];
   final[i]=mutableDict;
}