Reputation: 14835
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?
Upvotes: 1
Views: 4243
Reputation: 7145
No it will not automatically convert them. You have to do that yourself. You'll definitely get the unrecognized selector sent to instance exception.
Upvotes: 0
Reputation: 4398
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];
}
Upvotes: 4