开发者

Accessing two levels in my plist file

I have a plist file that contains country, state, city. I can access the country level for the pickers, but I cannot seem to get the state and city populated. I have created a plist file with the following: country --> dictionary, state--> dictionary, and city --> array.

The code is:

NSBundle *bundle = [NSBundle mainBundle];
N开发者_Python百科SString *plistPath = [bundle pathForResource:@"placesdictionary" ofType:@"plist"];
NSDictionary *dictionary = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
self.places = dictionary;
NSArray *componentsCountry = [self.places allKeys];
NSArray *sortedCountry = [componentsCountry sortedArrayUsingSelector:@selector(compare:)];
self.country = sortedCountry;
NSString *selectedCountry = [country objectAtIndex:0];

This is fine. But the following code has errors:

NSArray *componentsState = [self.places allKeysForObject:selectedCountry];
NSArray *sortedState = [componentsState sortedArrayUsingSelector:@selector(compare:)];
self.state = sortedState;
NSString *selectedState = [state objectAtIndex:0];
NSArray *componentsCity = [self.places objectForKey:selectedState];
NSArray *sortedCity = [componentsCity sortedArrayUsingSelector:@selector(compare:)];
self.city = sortedCity;

Can you please help me with the problems? Thank you.

This is my plist file:

    <?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>France</key>
        <dict>
            <key>state 3</key>
            <array>
                <string>Paris</string>
            </array>
            <key>state 2</key>
            <array>
                <string>Nice</string>
            </array>
        </dict>
        <key>US</key>
        <dict>
            <key>california</key>
            <array>
                <string>LA</string>
                <string>San Fran</string>
                <string>San Diego</string>
            </array>
            <key>new jersey</key>
            <array>
                <string>riverdale</string>
                <string>newark</string>
                </array>
        </dict>
     </dict>
    </plist>


I would advice to use such code for accessing plist data:

NSString *errorDesc = nil;
NSPropertyListFormat format;
NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:[[NSBundle mainBundle] pathForResource:@"placesdictionary" ofType:@"plist"]];
NSMutableDictionary *properties = (NSMutableDictionary *)[NSPropertyListSerialization propertyListFromData:plistXML mutabilityOption:NSPropertyListMutableContainersAndLeaves format:&format errorDescription:&errorDesc];
NSDictionary *france = (NSDictionary *)[properties valueForKey:@"France"]; 
// if cities is in countries then you can access them using [countries valueForKey:@"cityName"];
NSArray *cities = [france valueForKey:@"state 3"];
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜