访问我的plist文件中的两个级别 [英] Accessing two levels in my plist file

查看:60
本文介绍了访问我的plist文件中的两个级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含国家,州和城市的plist文件.我可以访问选择者所在的国家/地区级别,但似乎无法填充州和城市.我用以下内容创建了一个plist文件:country->字典,state->字典和city->数组.

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.

代码是:

NSBundle *bundle = [NSBundle mainBundle];
NSString *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.

这是我的plist文件:

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>

推荐答案

我建议使用此类代码访问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"];

这篇关于访问我的plist文件中的两个级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆