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!
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:
Abstract all data associated with code
into a separate class (perhaps called Person
) and use instances of this class as values in the dictionary.
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.
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!
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]];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With