Cherr Skees
Cherr Skees

Reputation: 1518

Data structure layout - NSMutableDictionary

I’m looking for the best way to store data like this...

Value 1 -item 1 -item 2 -item 3 ... -item 9 Value 2 -item 1 ... -item 9 Value 3 etc...

Then I want to pick a subset of the items for a given “value”

Is NSMutableDictionary the way to go for this? I’m getting a little confused in setting this up. I’ve been trying this... but its not quite right apparently. Thanks for the help.

NSMutableDictionary *dictionary;
[dictionary setObject:@"Entry1" forKey:@"1"];
[dictionary setObject:@"1-Entry2" forKey:@"1"];
[dictionary setObject:@"Entry2" forKey:@"2"];
[dictionary setObject:@"Entry3" forKey:@"3"];

NSLog(@"1: %@", [dictionary objectForKey:@"1"]);
NSLog(@"/n 2: %@", [dictionary objectForKey:@"1"]);

Upvotes: 0

Views: 403

Answers (1)

mckeed
mckeed

Reputation: 9828

If you want multiple objects under a single key, you need to use arrays inside the dictionary:

NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject:[NSArray arrayWithObjects:@"item1", @"item2", nil] forKey:@"Key1"];

and so on.

Upvotes: 2

Related Questions