Objective-C 编程中单键保存多个值的 NSMutableDictionary
请告诉我如何在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
看起来您正在使用
code
作为键,并且您希望基于code
表示多个值。在这种情况下,您应该:将与
code
关联的所有数据抽象到一个单独的类(可能称为Person
)中,并使用此类的实例作为字典。使用多层字典:
It seems like you are using
code
as the key and you want to represent multiple values based oncode
. In that case, you should either:Abstract all data associated with
code
into a separate class (perhaps calledPerson
) and use instances of this class as values in the dictionary.Use more than one layer of dictionaries:
如果您确实坚持将对象存储在字典中,并且正在处理字符串,则可以始终将所有字符串附加在一起,并用逗号分隔,然后当您从键检索对象时,您将拥有所有字符串准 csv 格式的对象!然后,您可以轻松地将字符串解析为对象数组。
以下是您可以运行的一些示例代码:
然后检索并解析字典中的对象:
它可能不像 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:
And then to retrieve and parse the object in the dictionary:
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.
现代语法更加清晰。
A. 如果您在加载时构建静态结构:
B. 如果您实时添加项目(可能):
那么您将作为 2D 字典进行访问...
Modern syntax is much cleaner.
A. If you're building a static structure at load time:
B. If your adding items in real time(probably):
Then you access as a 2D dictionary...