NSMutableDictionary 在 Objective-C 编程中使用单个键保存多个值 [英] NSMutableDictionary with single key holding many values in Objective-C programming

查看:58
本文介绍了NSMutableDictionary 在 Objective-C 编程中使用单个键保存多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

就我而言:

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

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

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.

谢谢!

推荐答案

好像你在使用 code 作为键,你想根据 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. 将与 code 关联的所有数据抽象到一个单独的类(可能称为 Person)中,并使用该类的实例作为字典中的值.

  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.

使用多于一层的字典:

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.

这篇关于NSMutableDictionary 在 Objective-C 编程中使用单个键保存多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆