如何从 plist 创建一个每个键有两个值的 NSDictionary

发布于 2024-09-11 07:29:37 字数 358 浏览 4 评论 0原文

我有一个依赖的 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 技术交流群。

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

发布评论

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

评论(2

雨落□心尘 2024-09-18 07:29:37

理想的结构实际上是拥有一本字典的字典。

A = {
  A1 = 36;
  A2 = 42;
  A3 = 89;
};
B = {
  B1 = 64;
  ...

plist 只是序列化格式(的集合)。没看懂怎么被打败的


要存储键值对值,您有两种选择。

(1) 创建一个新类。

@interface StringNumberPair : NSObject {
  NSString* strval;
  int intval;
}
@property(copy) NSString* strval;
@property(assign) int intval;
-(id)initWithString:(NSString*)strval_ integer:(int)intval_;
@end
...

[theDict setObject:[[[StringNumberPair alloc] initWithString:@"string"
                                                     integer:42] autorelease]
            forKey:@"key"];
  • Pro — 清晰的界面。
  • 缺点——你需要编写大量代码。

(2) 将值设置为数组:

[theDict setObject:[NSArray arrayWithObjects:
                            @"string", [NSNumber numberWithInt:42], nil]
            forKey:@"key"];

或字典:

[theDict setObject:[NSDictionary dictionaryWithObjectsAndKeys:
                      @"string",                   @"strval",
                      [NSNumber numberWithInt:42], @"intval",
                      nil]
            forKey:@"key"];
  • Pro — 这些是存储结构化值的标准类型。它很容易序列化为 plist。
  • 缺点——数组和字典太灵活了。

The ideal structure is actually to have a dictionary of dictionaries.

A = {
  A1 = 36;
  A2 = 42;
  A3 = 89;
};
B = {
  B1 = 64;
  ...

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.

@interface StringNumberPair : NSObject {
  NSString* strval;
  int intval;
}
@property(copy) NSString* strval;
@property(assign) int intval;
-(id)initWithString:(NSString*)strval_ integer:(int)intval_;
@end
...

[theDict setObject:[[[StringNumberPair alloc] initWithString:@"string"
                                                     integer:42] autorelease]
            forKey:@"key"];
  • Pro — Clear interface.
  • Cons — You need to write a lot of code.

(2) Make the value an array:

[theDict setObject:[NSArray arrayWithObjects:
                            @"string", [NSNumber numberWithInt:42], nil]
            forKey:@"key"];

or a dictionary:

[theDict setObject:[NSDictionary dictionaryWithObjectsAndKeys:
                      @"string",                   @"strval",
                      [NSNumber numberWithInt:42], @"intval",
                      nil]
            forKey:@"key"];
  • Pro — These are standard types to store structured values. It is easily serializable to a plist.
  • Cons — Arrays and dictionary are too flexible.
冷清清 2024-09-18 07:29:37

你说你的值是一个字符串数组,对吗?

因此,您可以将数字常量转换为字符串

[NSString stringWithFormat:@"%d"numericConstant]

并将其添加到数组中。

当您从字典中获取它时,将其转换回 int

[stringConstant intValue]

You said your value is an array of strings right?

So you can convert the numeric constant to string

[NSString stringWithFormat:@"%d"numericConstant]

and add it to the array.

Convert it back to int when you fetch it from dictionary

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