NSDictionary PList 中的 iPhone 对象

发布于 2024-08-18 06:40:35 字数 2114 浏览 6 评论 0原文

在我的应用程序中,我有一个部分从保存的 plist 加载,该 plist 有 2 个嵌套字典,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>1</key>
    <dict>
        <key>color</key>
        <string>yellow</string>
        <key>lang</key>
        <string>US</string>
        <key>name</key>
        <string>Peter</string>
        <key>uid</key>
        <string>1</string>
    </dict>
    <key>2</key>
    <dict>
        <key>color</key>
        <string>blue</string>
        <key>lang</key>
        <string>US</string>
        <key>name</key>
        <string>Josh</string>
        <key>uid</key>
        <string>2</string>
    </dict>
    <key>3</key>
    <dict>
        <key>color</key>
        <string>red</string>
        <key>lang</key>
        <string>DE</string>
        <key>name</key>
        <string>Susan</string>
        <key>uid</key>
        <string>3</string>
    </dict>
</dict>
</plist>

现在我想从键 2 访问类似外部字典的字符串,内部键颜色的值(蓝色) 我尝试制作 2 个循环,它适用于外部字典,但我无法访问内部字典

NSMutableDictionary *savedData = [NSMutableDictionary dictionaryWithContentsOfFile:path]; // This contains all data from plist

for (int x=0; x<[savedData count]; x++) {
    NSString *itemNumber = [NSString stringWithFormat:@"%d", x+1];

    //This prints out the correct inner dictionary
    NSLog(@"item#%@: %@",itemNumber,[savedData objectForKey:itemNumber]);


    for (NSDictionary *dict in [savedData objectForKey:itemNumber]) {
        //prints out color, lang, uid, but no key-value pairs 
        NSLog(@"dict: %@",dict);
    }
}

我想知道如何直接访问内部字典内的键值对,任何人都可以给我一个正确的方向,请?

in my app I have a section where I load from a saved plist which has 2 nested dictionaries like this:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>1</key>
    <dict>
        <key>color</key>
        <string>yellow</string>
        <key>lang</key>
        <string>US</string>
        <key>name</key>
        <string>Peter</string>
        <key>uid</key>
        <string>1</string>
    </dict>
    <key>2</key>
    <dict>
        <key>color</key>
        <string>blue</string>
        <key>lang</key>
        <string>US</string>
        <key>name</key>
        <string>Josh</string>
        <key>uid</key>
        <string>2</string>
    </dict>
    <key>3</key>
    <dict>
        <key>color</key>
        <string>red</string>
        <key>lang</key>
        <string>DE</string>
        <key>name</key>
        <string>Susan</string>
        <key>uid</key>
        <string>3</string>
    </dict>
</dict>
</plist>

Now I want to access string like outer dictionary from key 2, value for inner key color (blue)
I tried make 2 loops, and it works for the outer dictionary but I can't access the inner

NSMutableDictionary *savedData = [NSMutableDictionary dictionaryWithContentsOfFile:path]; // This contains all data from plist

for (int x=0; x<[savedData count]; x++) {
    NSString *itemNumber = [NSString stringWithFormat:@"%d", x+1];

    //This prints out the correct inner dictionary
    NSLog(@"item#%@: %@",itemNumber,[savedData objectForKey:itemNumber]);


    for (NSDictionary *dict in [savedData objectForKey:itemNumber]) {
        //prints out color, lang, uid, but no key-value pairs 
        NSLog(@"dict: %@",dict);
    }
}

I'd like to know how to directly access key value pairs inside the inner dictionary, could anyone give me a kick in the right direction, please?

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

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

发布评论

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

评论(3

守望孤独 2024-08-25 06:40:35

怎么样:

NSString *blueColorString = [[savedData objectForKey:@"2"] objectForKey:@"color"];

技巧是嵌套方法调用,第一个 [savedData objectForKey:2] 返回您可以再次调用该方法的内部字典对象。希望这能按预期工作。

最好的,
罗宾

How about this:

NSString *blueColorString = [[savedData objectForKey:@"2"] objectForKey:@"color"];

The trick is to nest the method calls, the first one [savedData objectForKey:2] returns you the inner dictionary object on which you can call the method again. Hope this works as intended.

Best,
Robin

メ斷腸人バ 2024-08-25 06:40:35

NSLog(@"%@", [savedData valueForKeyPath:@"1.color"]);

黄色

NSLog(@"%@", [savedData valueForKeyPath:@"1.color"]);

yellow

感悟人生的甜 2024-08-25 06:40:35

通常,当您的键是字符串时,您将使用 valueForKey:

// to get to "blue" string
NSLog(@"outer key '2', inner key 'color' = %@",[[savedData valueForKey:@"2"] valueForKey:@"color"]);

// all inner key / value pairs
for (NSString *key in savedData) {
    NSDictionary *innerDict = [savedData valueForKey:key];

    for (NSString *innerKey in innerDict) {
        NSLog(@"key %@ value %@",innerKey,[innerDict valueForKey:innerKey]);
    }
}

Typically, you would use valueForKey: when your keys are strings:

// to get to "blue" string
NSLog(@"outer key '2', inner key 'color' = %@",[[savedData valueForKey:@"2"] valueForKey:@"color"]);

// all inner key / value pairs
for (NSString *key in savedData) {
    NSDictionary *innerDict = [savedData valueForKey:key];

    for (NSString *innerKey in innerDict) {
        NSLog(@"key %@ value %@",innerKey,[innerDict valueForKey:innerKey]);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文