Ryasoh
Ryasoh

Reputation: 173

Get 'unrecognized selector' when trying to add dictionary to nsMutableArray

I have an NSMutableArray that prints something like:

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

I'm looping through it to add another key here:

 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];

When I print final 'i' get:

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

But when i try to add entries I get the error: -[__NSDictionaryI addEntriesFromDictionary:]: unrecognized selector sent to instance 0x7b803330'

How can I add the dictionary so final is: final is { user = "Mark"; age = 21; car = "Ford"; }

I've tried using NSDictionary instead of NSMutableDictionary. I'm not sure if changing NSMutableArray to NSMutableDictionary would work or if it's possible (haven't found a solution on SO yet).

Upvotes: 0

Views: 495

Answers (1)

Paulw11
Paulw11

Reputation: 114846

The dictionaries stored in your final array are immutable. You need to replace them with a mutable dictionary that contains the merged objects -

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;
}

Upvotes: 0

Related Questions