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.