Objective-C 编程中单键保存多个值的 NSMutableDictionary

发布于 2024-09-13 14:44:14 字数 461 浏览 4 评论 0原文

请告诉我如何在 NSMutableDictionary 中为同一个键拥有多个值?

当我使用以下方法时,这些值将被替换为最近的值。

就我而言:

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

当我查看字典的内容时,我仅获得关键代码的 reminderDate 。在这里,所有值的代码都是相同的。如何避免名字和姓氏被 plannedReminder 替换。

谢谢你!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

葬﹪忆之殇 2024-09-20 14:44:14

看起来您正在使用 code 作为键,并且您希望基于 code 表示多个值。在这种情况下,您应该:

  1. 将与 code 关联的所有数据抽象到一个单独的类(可能称为 Person)中,并使用此类的实例作为字典。

  2. 使用多层字典:

    NSMutableDictionary *dictionary = [NSMutableDictionary 字典];
    
    NSMutableDictionary *firstOne = [NSMutableDictionary 字典];
    [firstOne setObject:forename forKey:@"forename"];
    [firstOne setObject:姓氏 forKey:@"姓氏"];
    [firstOne setObject:reminderDate forKey:@"reminderDate"];
    
    [字典 setObject:firstOne forKey:[NSNumber numberWithInt:code]];
    
    // 对每个条目重复。
    

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.
    
森林散布 2024-09-20 14:44:14

如果您确实坚持将对象存储在字典中,并且正在处理字符串,则可以始终将所有字符串附加在一起,并用逗号分隔,然后当您从键检索对象时,您将拥有所有字符串准 csv 格式的对象!然后,您可以轻松地将字符串解析为对象数组。

以下是您可以运行的一些示例代码:

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];

然后检索并解析字典中的对象:

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

它可能不像 dreamlax 建议的那样具有多层字典那么干净,但是如果您正在处理想要存储键的数组,并且该数组中的对象本身没有特定的键,这是一个解决方案!

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!

凉墨 2024-09-20 14:44:14

我认为你不明白字典是如何工作的。每个键只能有一个值。您需要一个字典的字典或数组的字典。

在这里,您为每个人创建一个字典,然后将其存储在您的主字典中。

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

[dictionary setObject:d forKey:[NSNumber numberWithint:code]];

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]];
审判长 2024-09-20 14:44:14

现代语法更加清晰。

A. 如果您在加载时构建静态结构:

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

B. 如果您实时添加项目(可能):

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

那么您将作为 2D 字典进行访问...

mDic[code][@"forename"];

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"];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文