Domanda

I have the following code:

for (NSMutableDictionary *aDict in array)
{
     // do stuff
     [aDict setObject:myTitle forKey:@"title"];
}

My question is, if the array is filled with NSDictionary objects, will this for loop code as written automatically convert them into NSMutableDictionary objects?

Or do I need to do something more specific here to ensure that I don't get an unrecognized selector sent to instance error on setObject:forKey: in the loop?

È stato utile?

Soluzione

Currently that will give you the error you mentioned. Whilst the loop is setup with mutable dictionaries, the underlying object is still immutable. You'd need to create a new dictionary out of it. Try this

NSMutableArray *newArray = [NSMutableArray array];
for (NSDictionary *aDict in array)
{
     NSMutableDictionary *mutable = [aDict mutableCopy];
     // do stuff
     [mutable setObject:myTitle forKey:@"title"];
     [newArray addObject:mutable];
}

Altri suggerimenti

No it will not automatically convert them. You have to do that yourself. You'll definitely get the unrecognized selector sent to instance exception.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top