开发者

Assessing elements in plist iphone sdk

ok i have a plist like this

`
<dict>
<key>Rows</key>
<array>
    <dict>
        <key>WireSize</key>
        <string>16 AWG</string>
        <key>Children</key>
        <array>
            <dict>
                <key>Cooper 60°C (140°F)</key>
                <string>0</string>
            </dict>
            <dict>
                <key>Cooper 75°C (167°F)</key>
                <string>0</string>
            </dict>
            <dict>
                <key>Cooper 90°C (194°F)</key>
                <string>14</string>
            </dict>
            <dict>
                <key>Aluminum 60°C (140°F)</key>
                <string>0</string>
            </dict>
            <dict>
                <key>Aluminum 75°C (167°F)</key>
                <string>0</string>
            </dict>
            <dict>
                <key>Aluminum 90°C (194°F)</key>
                <string>0</string>
            </dict>
        </array>
    </dict>
    <dict>
        <key>WireSize</key>
        <string>16 AWG</string>
        <key>Children</key>
        <array>
            <dict>
                <key>Cooper 60°C (140°F)</key>
                <string>0</string>
            </dict>
            <dict>
                <key>Cooper 75°C (167°F)</key>
                <string>0</string>
            </dict>
            <dict>
                <key>Cooper 90°C (194°F)</key>
                <string>14</string>
            </dict>
            <dict>
                <key>Aluminum 60°C (140°F)</key>
                <string>0</string>
            </dict>
            <dict>
                <key>Aluminum 75°C (167°F)</key>
                <string>0</string>
            </dict>
            <dict>
                <key>Aluminum 90°C (194°F)</key>
                <string>0</string>
            </dict>
        </array>
    </dict>

</array>

`

and been trying to read the values from it but not success

i am using this code

enter NSBundle *bundle = [NSBundle mainBundle];
NSString *plistPath = [bundle pathForResource:@"Table 310-16" ofType:@"plist"];
NSDictionary *dictionary = [[NSDictionary alloc] initWithContentsOfFile:plistPath];

for (id key in dictionary) {
    NSArray *array = [dictionary objectForKey:key];
    NSLog(@"key: %@, value: %@", key, [array objectAtIndex:0]);
} here

and the results are

key: Rows, value: {
Children =     (
            {
        "Cooper 60\U00b0C (140\U00b0F)" = 0;
    },
            {
        "Cooper 75\U00b0C (167\U00b0F)" = 0;
    },
            {
        "Cooper 90\U0开发者_JAVA技巧0b0C (194\U00b0F)" = 14;
    },
            {
        "Aluminum 60\U00b0C (140\U00b0F)" = 0;
    },
            {
        "Aluminum 75\U00b0C (167\U00b0F)" = 0;
    },
            {
        "Aluminum 90\U00b0C (194\U00b0F)" = 0;
    }
);
WireSize = "16 AWG";

}

but still don't know how to get and specific value for example Aluminum 60°C (140°F) or 14 or 16 AWG any help would be appresiated

HP


NSDictionary keys must be NSString objects. Given that the example for loop uses id key, I'll bet you're used to a language like ruby that allows keys to be an arbitrary object type. Since the keys must be NSStrings, the following will always work:

for (NSString *key in dictionary) {
    NSLog(@"key: %@, value: %@", key, [dictionary objectForKey:key]);
}

You can pass the string directly. So, to get a specific value such as your example:

// Edit: objectForValue: should have been objectForKey:
NSLog(@"Value for Aluminum 60°C (140°F): %@", [dictionary objectForKey:@"Aluminum 60°C (140°F)"]);


If you want to do arbitrary lookups from a given key, you'll simply need to write some helper methods to iterate through your plist data structure, or perhaps a bit cleaner is to create a couple classes to encapsulate those data structures and create a tree of those classes when the app starts up.

It also seems like you could simplify the data structure a bit. something like this perhaps?

<array>
    <dict>
        <key>16 AWG</key>
        <array>
                <dict>
                        <key>Cooper 60°C (140°F)</key>
                        <string>0</string>
                </dict>
                <dict>
                        <key>Cooper 75°C (167°F)</key>
                        <string>0</string>
                </dict>
        </array>
    </dict>
    <dict>
        <key>12 AWG</key>
        <array>
                <dict>
                        <key>Cooper 60°C (140°F)</key>
                        <string>0</string>
                </dict>
                <dict>
                        <key>Cooper 75°C (167°F)</key>
                        <string>0</string>
                </dict>
        </array>
    </dict>
</array>

So you can then presume that all the keys in the top-level array of dicts are wire gauges, and the values for each key are the wire options.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜