嵌套字典 - 针对单个键

发布于 2024-11-15 05:43:48 字数 1527 浏览 4 评论 0原文

我很高兴这里有很多此类问题,但我无法找到可行的答案。

简而言之,我需要获取所选类别中所有产品的一系列价格。

这是我的 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 技术交流群。

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

发布评论

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

评论(1

反差帅 2024-11-22 05:43:48

currentDetail 仅显示
产品 1、产品 2 等

由于 NSDictionary 的快速枚举会迭代键,因此您要将键添加到数组中。因此,您的输出显示产品 1、产品 2 等。

我需要获取一系列价格
所选类别内的所有产品。

您需要使用这些键(Product 1、Product 2 等)来检索关联的产品字典,然后检索与 Price 键关联的值:

NSDictionary *detailDict = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"detail" ofType:@"plist"]];
NSDictionary *categoryDict = [detailDict objectForKey:@"Product Category"];
NSMutableArray *pricesArray = [NSMutableArray arrayWithCapacity:[categoryDict count]];
for (NSString *key in categoryDict) {
    [pricesArray addObject:[[categoryDict objectForKey:key] objectForKey:@"Price"]];
}

请记住,生成的 pricesArray 位于 <没有特定的顺序。

currentDetail just shows
Product 1, Product 2 etc.

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.

I need to get an array of Prices for
all products within a chosen category.

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:

NSDictionary *detailDict = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"detail" ofType:@"plist"]];
NSDictionary *categoryDict = [detailDict objectForKey:@"Product Category"];
NSMutableArray *pricesArray = [NSMutableArray arrayWithCapacity:[categoryDict count]];
for (NSString *key in categoryDict) {
    [pricesArray addObject:[[categoryDict objectForKey:key] objectForKey:@"Price"]];
}

Bear in mind that the resulting pricesArray is in no specific order.

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