如何访问字典数组中的特定键值

发布于 2024-10-21 06:00:07 字数 1443 浏览 3 评论 0原文

如何从特定键访问单个键值?

我正在使用 NSDictionary 的 NSArray 和 plist 源,如下所示;

<plist version="1.0">
<array>
<dict>
    <key>name</key>
    <string>item name</string>
    <key>level</key>
    <integer>3</integer> // range is 1 .. 7
    <key>Stage1</key>
    <string>Text for Stage 1</string>
    <key>Stage2</key>
    <string>Text for Stage 2</string>
    <key>Stage3</key>
    <string>Text for Stage 3</string>
    <key>Stage4</key>
    <string>Text for Stage 4</string>
    <key>Stage5</key>
    <string>Text for Stage 5</string>
    <key>Stage6</key>
    <string>Text for Stage 6</string>
    <key>Stage7</key>
    <string>Text for Stage 7</string>
</dict>
// more dictionaries follow

我可以通过执行以下操作来查看整个词典:

NSNumber *helps = [myArray objectAtIndex:[pickerView selectedRowInComponent:0]]; 
notesLabel.text = [NSString stringWithFormat:@"%@", helps];

非常感谢您的帮助......我真是个菜鸟。

至此,感谢您的帮助!我能够补充:

NSDictionary *dictionary = [foodArray objectAtIndex:[pickerView selectedRowInComponent:0]];
notesLabel.text = (@"stage is: %@", [dictionary objectForKey:[NSString stringWithFormat:@"%@", stage]]);

这正是我所缺少的。 :)

How do I access a single key value from a specific key?

I am using an NSArray of NSDictionary with a plist source as shown below;

<plist version="1.0">
<array>
<dict>
    <key>name</key>
    <string>item name</string>
    <key>level</key>
    <integer>3</integer> // range is 1 .. 7
    <key>Stage1</key>
    <string>Text for Stage 1</string>
    <key>Stage2</key>
    <string>Text for Stage 2</string>
    <key>Stage3</key>
    <string>Text for Stage 3</string>
    <key>Stage4</key>
    <string>Text for Stage 4</string>
    <key>Stage5</key>
    <string>Text for Stage 5</string>
    <key>Stage6</key>
    <string>Text for Stage 6</string>
    <key>Stage7</key>
    <string>Text for Stage 7</string>
</dict>
// more dictionaries follow

I can view the whole dictionary by doing the following:

NSNumber *helps = [myArray objectAtIndex:[pickerView selectedRowInComponent:0]]; 
notesLabel.text = [NSString stringWithFormat:@"%@", helps];

Your help is greatly appreciated ... I am such a noob.

Till, thanks for the help! I was able to add:

NSDictionary *dictionary = [foodArray objectAtIndex:[pickerView selectedRowInComponent:0]];
notesLabel.text = (@"stage is: %@", [dictionary objectForKey:[NSString stringWithFormat:@"%@", stage]]);

Which gives me exactly what I was missing. :)

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

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

发布评论

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

评论(1

黒涩兲箜 2024-10-28 06:00:07

检查 NSDictionary< 上的文档/a> - 专门用于“objectForKey:”

NSDictionary *dictionary = [myArray objectAtIndex:[pickerView selectedRowInComponent:0]];
NSLog(@"name: %@", [dictionary objectForKey:@"name"]);
NSLog(@"level: %@", [dictionary objectForKey:@"level"]);

Check the documentation on NSDictionary - specifically for "objectForKey:"

NSDictionary *dictionary = [myArray objectAtIndex:[pickerView selectedRowInComponent:0]];
NSLog(@"name: %@", [dictionary objectForKey:@"name"]);
NSLog(@"level: %@", [dictionary objectForKey:@"level"]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文