djt9000 djt9000 - 3 months ago 8
Objective-C Question

NSMutable Dictionary adding objects

Is there a more efficient way to add objects to an NSMutable Dictionary than simple iteration?


// Create the dictionary

NSMutableDictionary *myMutableDictionary = [NSMutableDictionary dictionary];

// Add the entries

[myMutableDictionary setObject:@"Stack Overflow" forKey:@""];
[myMutableDictionary setObject:@"SlashDotOrg" forKey:@""];
[myMutableDictionary setObject:@"Oracle" forKey:@""];

Just curious, I'm sure that this is the way it has to be done.


If you have all the objects and keys beforehand you can initialize it using NSDictionary's:


Of course this will give you immutable dictionary not mutable. It depends on your usage which one you need, you can get a mutable copy from NSDictionary but it seems easier just to use your original code in that case:

NSDictionary * dic = [NSDictionary dictionaryWith....];
NSMutableDictionary * md = [dic mutableCopy];
... use md ...
[md release];