NSMutableDictionary with single key holding many v

2020-07-06 07:41发布

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!

4条回答
Anthone
2楼-- · 2020-07-06 08:01

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]];
查看更多
贼婆χ
3楼-- · 2020-07-06 08:02

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"];
查看更多
我命由我不由天
4楼-- · 2020-07-06 08:09

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.
    
查看更多
5楼-- · 2020-07-06 08:12

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!

查看更多
登录 后发表回答