5

How do I add multiple objects to same key in an NSMutableDictionary? I cannot find the right method. The setObject method only updates a single object in the key array. The method addObject: forkey: in the NSMutableDictionary isn't available and is causing a crash when it is used.

The dictionary is read from a plist file.

The Dictionary:

temp = {
    nickname : score 
        item 0 = level1;
        item 1 = level2;
        item 3 = level3;
    score 
        item 0 = 400;
        item 1 = 400;
        item 3 = 400;
}

Here is the code:

NSMutableDictionary *newDict = [[NSMutableDictionary alloc] init];
str_nickname = [temp objectForKey:@"nickname"];
for (NSString *key in str_nickname){
    if ([key isEqualToString:@"level2"]) {  //replace object with new name
        [newDict setObject:@"new level" forKey:@"nickname"];
    } else {
        [newDict addObject:key forKey:@"nickname"];   //wont work!!!
    }
}

Also I want to update the new score in the new dictionary and have to update this at the corresponding level-object, maybe by the index?

HAS
  • 19,140
  • 6
  • 31
  • 53
Johan Sneek
  • 49
  • 1
  • 6
  • Have you defined your data structures well? If so, what does that design look like? Knowing that it would be much easier to help you. – Monolo May 22 '13 at 13:20
  • 1
    Think about this: after you set two different objects for the same key, what do you want the dictionary to give you when you ask for the `objectForKey:`? – jscs May 22 '13 at 18:48

2 Answers2

2

if you want to have multiple objects stored under the same key in a dictionary, your only chance is putting them into an

  • array
  • other dictionary
  • set
  • bag
  • any collection type you fancy

and storing that in your dictionary. Reason is, dictionaries are key-value-PAIRINGS. The entire architecture isnt made to support a key that corresponds to more than one object. The objects would be indistuguishable for the system, hence, no dice.

EDIT: If you want to access the object by using an index, i guess your best bet is the Array-Version. Store a Mutable Array in your dictionary for the key "nickname", then add Objects to that array. To Store:

[myDictionary setObject:[NSMutableArray array] ForKey:@"nickname"];
[[myDictionary objectForKey:@"nickname"] addObject:yourObject];

To retrieve:

[[myDictionary objectForKey:@"nickname"] objectAtIndex:index];
Regexident
  • 29,441
  • 10
  • 93
  • 100
katzenhut
  • 1,742
  • 18
  • 26
1

if your plist file is like this "test.plist"

The Dictionary temp={
    nickname : Junaid Sidhu

    levels 
             item 0 = level1;
             item 1 = level2;
             item 3 = level3;
    score 
             item 0 = 400;
             item 1 = 400;
             item 3 = 400;
    }

Here is the code

NSDictionary *temp = [[NSDictionary alloc]initWithContentsOfFile:[NSBundle mainBundle] pathForResource:@"test" ofType:@"plist"]];

NSMutableDictionary *newDict = [[NSMutableDictionary alloc] init];

NSString *str_nickname = [temp objectForKey:@"nickname"];
NSMutableArray *levels = [NSMutableArray new];
NSMutableArray *score =  [NSMutableArray new];

for (NSString *key in [temp allKeys]){

    if ( [key isEqualToString:@"levels"]) {  //replace object with new name

           levels = [NSMutableArray arrayWithArray:(NSArray *)[temp objectForKey:key]];

    }
    else if ( [key isEqualToString:@"score"]){

           score = [NSMutableArray arrayWithArray:(NSArray *)[temp objectForKey:key]];

    }
}

NSLog(@"Nick : %@ \n levels : %@ \n score : %@ \n",str_nickname,levels,score)
junaidsidhu
  • 3,539
  • 1
  • 27
  • 49
  • i want to use a plist file for storing level scores of a puzzle game. I tried this with NSUserdefaults, which works more or less with the same structure. My question: Am i on the right path, or should i use another method for storing game data? I am pretty new with xcode, but i have search for hours – Johan Sneek May 22 '13 at 20:55