嵌套字典 - 针对单个键
我很高兴这里有很多此类问题,但我无法找到可行的答案。
简而言之,我需要获取所选类别中所有产品的一系列价格。
这是我的 pList:
<key>Product Category</key>
<dict>
<key>Product 1</key>
<dict>
<key>Image</key>
<string></string>
<key>Large Image</key>
<string></string>
<key>Detail</key>
<string></string>
<key>Price</key>
<integer>100</integer>
</dict>
<key>Product 2</key>
<dict>
<key>Image</key>
<string></string>
<key>Large Image</key>
<string></string>
<key>Detail</key>
<string></string>
<key>Price</key>
<integer>200</integer>
</dict>
</dict>
我不知道如何在这样的层次结构中深入定位。这是我到目前为止的尝试:
NSString *detailPListPath = [[NSBundle mainBundle] pathForResource:@"detail" ofType:@"plist"];
NSDictionary *detailDictionary = [NSDictionary dictionaryWithContentsOfFile:detailPListPath];
currentDetail = [[NSMutableArray alloc] init];
for (id object in [detailDictionary objectForKey:indication]) {
[currentDetail addObject:object];
}
但是 currentDetail
仅显示产品 1、产品 2 等。
感谢您提供的任何帮助!
I appreciate there are lots of this type of question around here but I've not been able to find a feasible answer.
Put simply, I need to get an array of Prices for all products within a chosen category.
Here is my pList:
<key>Product Category</key>
<dict>
<key>Product 1</key>
<dict>
<key>Image</key>
<string></string>
<key>Large Image</key>
<string></string>
<key>Detail</key>
<string></string>
<key>Price</key>
<integer>100</integer>
</dict>
<key>Product 2</key>
<dict>
<key>Image</key>
<string></string>
<key>Large Image</key>
<string></string>
<key>Detail</key>
<string></string>
<key>Price</key>
<integer>200</integer>
</dict>
</dict>
I have no idea how to target deep within a hierarchy like this. Here is my attempt so far:
NSString *detailPListPath = [[NSBundle mainBundle] pathForResource:@"detail" ofType:@"plist"];
NSDictionary *detailDictionary = [NSDictionary dictionaryWithContentsOfFile:detailPListPath];
currentDetail = [[NSMutableArray alloc] init];
for (id object in [detailDictionary objectForKey:indication]) {
[currentDetail addObject:object];
}
But then currentDetail
just shows Product 1, Product 2 etc.
Thanks for any help you can offer!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于
NSDictionary
的快速枚举会迭代键,因此您要将键添加到数组中。因此,您的输出显示产品 1、产品 2 等。您需要使用这些键(Product 1、Product 2 等)来检索关联的产品字典,然后检索与 Price 键关联的值:
请记住,生成的
pricesArray
位于 <没有特定的顺序。Since fast enumeration of a
NSDictionary
iterates over the keys, you are adding the keys to the array. Hence, your output shows Product 1, Product 2, etc.You need to use these keys (Product 1, Product 2, etc.) to retrieve the associated product dictionary and then retrieve the value associated with the Price key:
Bear in mind that the resulting
pricesArray
is in no specific order.