如何从 plist 创建一个每个键有两个值的 NSDictionary
我有一个依赖的 UIPicker,它将值从 plist 加载到它的两个组件中。它是一个带有 NSArrays
字符串的 NSDictionary
。用户选择一对组件,选择器将这两个字符串发送回委托。我真正需要的是一个与每个人的选择相一致的常数。例如,第一个组件是 A、B、C,每个选项都有三个子选项 A1、A2、A3 等...用户选择 ChoiceA 和 ChoiceA3,选择器返回两个字符串,但我有一个关联的数字常量与 A-A3 这就是我的程序中实际需要的数学。
我可以有两个与特定键关联的东西 - 字符串和数字吗?我想我想要的是一个键值对(三元组)?似乎在我的程序中使用逻辑根据返回的键值对分配值就违背了使用 plist 的目的。
I have a dependent UIPicker
that loads the values into its two components from a plist. It's an NSDictionary
with NSArrays
of strings. The user selects a pair of components and the picker sends those two strings back to the delegate. What I really need is a constant that goes along with each individual choice. For example, first component is A, B, C and each choice has three subchoices A1, A2, A3, etc... User selects ChoiceA with ChoiceA3 and the picker returns the two strings fine, but I have a numeric constant that is associate with A-A3 and that is what I actually need for the math in my program.
Can I have two things associated with a particular key - a string AND a number? I guess what I want is a key-value-value pair (triplet)? It seems like using logic in my program to assign a value based on the key-value pair returned defeats the purpose of using a plist in the first place.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
理想的结构实际上是拥有一本字典的字典。
plist 只是序列化格式(的集合)。没看懂怎么被打败的
要存储键值对值,您有两种选择。
(1) 创建一个新类。
(2) 将值设置为数组:
或字典:
The ideal structure is actually to have a dictionary of dictionaries.
The plist is just a (collection of) serialization format. I don't see how it's defeated.
To store key-value-value, you have 2 choices.
(1) Create a new class.
(2) Make the value an array:
or a dictionary:
你说你的值是一个字符串数组,对吗?
因此,您可以将数字常量转换为字符串
并将其添加到数组中。
当您从字典中获取它时,将其转换回 int
You said your value is an array of strings right?
So you can convert the numeric constant to string
and add it to the array.
Convert it back to int when you fetch it from dictionary