如何访问字典数组中的特定键值
如何从特定键访问单个键值?
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
检查 NSDictionary< 上的文档/a> - 专门用于“objectForKey:”
Check the documentation on NSDictionary - specifically for "objectForKey:"