NSMutable Dictionary adding objects

djt9000 picture djt9000 · Jul 13, 2009 · Viewed 77.3k times · Source

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.

Answer

Andrew Johnson picture Andrew Johnson · Jul 13, 2009
NSDictionary *entry = [NSDictionary dictionaryWithObjectsAndKeys:
  [NSNumber numberWithDouble:acceleration.x], @"x",
  [NSNumber numberWithDouble:acceleration.y], @"y",
  [NSNumber numberWithDouble:acceleration.z], @"z",
  [NSDate date], @"date", 
  nil];