NSMutable Dictionary adding objects
Asked Answered
S

4

28

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

Example:

// Create the dictionary

NSMutableDictionary *myMutableDictionary = [NSMutableDictionary dictionary];    

// Add the entries

[myMutableDictionary setObject:@"Stack Overflow" forKey:@"http://stackoverflow.com"];
[myMutableDictionary setObject:@"SlashDotOrg" forKey:@"http://www.slashdot.org"];
[myMutableDictionary setObject:@"Oracle" forKey:@"http://www.oracle.com"];

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

Slang answered 12/7, 2009 at 22:45 Comment(0)
D
15

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

dictionaryWithObjects:forKeys:

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];
Dolabriform answered 12/7, 2009 at 22:57 Comment(2)
Thank you for the second opinion. I just wanted to be sure I was not missing anything and to see if there was something a bit more efficient / elegant.Slang
I was as well searching for an option to initialize NSMutableDictionary with objects and keys like you can do for NSDictionary but I can't find anything ...Dolabriform
M
20
NSDictionary *entry = [NSDictionary dictionaryWithObjectsAndKeys:
  [NSNumber numberWithDouble:acceleration.x], @"x",
  [NSNumber numberWithDouble:acceleration.y], @"y",
  [NSNumber numberWithDouble:acceleration.z], @"z",
  [NSDate date], @"date", 
  nil];
Muttonhead answered 12/7, 2009 at 22:59 Comment(0)
D
15

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

dictionaryWithObjects:forKeys:

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];
Dolabriform answered 12/7, 2009 at 22:57 Comment(2)
Thank you for the second opinion. I just wanted to be sure I was not missing anything and to see if there was something a bit more efficient / elegant.Slang
I was as well searching for an option to initialize NSMutableDictionary with objects and keys like you can do for NSDictionary but I can't find anything ...Dolabriform
R
8

Allow me to add some information to people that are starting.

It is possible to create a NSDictionary with a more friendly syntax with objective-c literals:

NSDictionary *dict = @{ 
    key1 : object1, 
    key2 : object2, 
    key3 : object3 };
Ruin answered 8/7, 2013 at 13:12 Comment(2)
should you end the list with a nil?Barbuto
@Barbuto No need :). The same with NSArrays literals :)Ruin
T
0
NSMutableDictionary *example = [[NSMutableDictionary alloc]initWithObjectsAndKeys:@5,@"burgers",@3, @"milkShakes",nil];

The objects come before the keys.

Thuja answered 22/7, 2016 at 2:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.