NSMutableDictionary with single key holding many values in Objective-C programming
Asked Answered
H

4

9

Please tell me how can we have multiple values for the same key in NSMutableDictionary?

When I use the below approach, the values are being replaced with the recent one.

In my case:

[dictionary setObject:forename forKey:[NSNumber numberWithint:code]];
[dictionary setObject:surname forKey:[NSNumber numberWithint:code]];
[dictionary setObject:reminderDate forKey:[NSNumber numberWithint:code]];

When I view the contents of the dictionary, I get only the reminderDate for key code. Here, the code is same for all values. How do I avoid forename and surname being replaced by plannedReminder.

Thank You!

Havoc answered 9/8, 2010 at 6:53 Comment(0)
J
16

It seems like you are using code as the key and you want to represent multiple values based on code. In that case, you should either:

  1. Abstract all data associated with code into a separate class (perhaps called Person) and use instances of this class as values in the dictionary.

  2. Use more than one layer of dictionaries:

    NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
    
    NSMutableDictionary *firstOne = [NSMutableDictionary dictionary];
    [firstOne setObject:forename forKey:@"forename"];
    [firstOne setObject:surname forKey:@"surname"];
    [firstOne setObject:reminderDate forKey:@"reminderDate"];
    
    [dictionary setObject:firstOne forKey:[NSNumber numberWithInt:code]];
    
    // repeat for each entry.
    
Jenifer answered 9/8, 2010 at 7:1 Comment(1)
Don't just downvote, let me know what's wrong so that I can fix it!Jenifer
P
5

If you are really adamant about storing objects in a dictionary, and if you are dealing with strings, you could always append all of your strings together separated by commas, and then when you retrieve the object from the key, you'll have all your objects in a quasi-csv format! You can then easily parse that string into an array of objects.

Here is some sample code you could run:

NSString *forename = @"forename";
NSString *surname = @"surname";
NSString *reminderDate = @"10/11/2012";
NSString *code = @"code";

NSString *dummy = [[NSString alloc] init];
dummy = [dummy stringByAppendingString:forename];
dummy = [dummy stringByAppendingString:@","];
dummy = [dummy stringByAppendingString:surname];
dummy = [dummy stringByAppendingString:@","];
dummy = [dummy stringByAppendingString:reminderDate];
dummy = [dummy stringByAppendingString:@","];
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
[dictionary setObject:dummy forKey:code];

And then to retrieve and parse the object in the dictionary:

NSString *fromDictionary = [dictionary objectForKey:code];
NSArray *objectArray = [fromDictionary componentsSeparatedByString:@","];
NSLog(@"object array: %@",objectArray);

It might not be as clean as having layers of dictionaries like dreamlax suggested, but if you are dealing with a dictionary where you want to store an array for a key and the objects in that array have no specific keys themselves, this is a solution!

Perfectly answered 14/7, 2012 at 1:53 Comment(1)
Why not simply use NSString *dummy = stringWithFormat:@"%@,%@,%@", forename, surname, reminderDate];. At the very least, you should be using an NSMutableString if you are concatenating in that way.Jenifer
J
2

I don't think you understand how dictionaries work. Each key can only have one value. You'll want to have a dictionary of dictionaries or a dictionary of arrays.

Here you create a dictionary for each person, and then store that in your master dictionary.

NSDictionary *d = [NSDictionary dictionaryWithObjectsAndKeys:
forename, @"forename", surname, @"surname", @reminderDate, "@reminderDate", nil];

[dictionary setObject:d forKey:[NSNumber numberWithint:code]];
Joktan answered 9/8, 2010 at 7:1 Comment(0)
C
1

Modern syntax is much cleaner.

A. If you're building a static structure at load time:

NSDictionary* dic = @{code : @{@"forename" : forename, @"surname" : surnamem, @"reminderDate" : reminderDate}/*, ..more items..*/};

B. If your adding items in real time(probably):

NSMutableDictionary* mDic = [[NSMutableDictionary alloc] init];
[mDic setObject:@{@"forename" : forename, @"surname" : surnamem, @"reminderDate" : reminderDate} forKey:code];
//..repeat

Then you access as a 2D dictionary...

mDic[code][@"forename"];
Companionway answered 22/5, 2015 at 17:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.