如何在plist中搜索以检索特定键的值 [英] How to search in a plist to retrieve a value for a specific key

查看:118
本文介绍了如何在plist中搜索以检索特定键的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个plist来存储我的应用程序用户的国家/地区数据.因此,我有一个<dict>列表,其中包含几个国家/地区.每个国家/地区都有几个.每个地区都有地区.每个地区拥有许多城市.

I am using a plist to store the country data for my app's users. So I have this <dict> list that holds several countries. For each country there are several provinces. For each region there are regions. And each region holds many cities.

所以我的列表如下:

<dict>
    <key>Country</key>
    <array>
        <dict>
            <key>ISO</key>
            <string>ES</string>
            <key>Value</key>
            <string>Spain</string>
            <key>Children</key>
            <array>
                <dict>
                    <key>ISO</key>
                    <string>AL</string>
                    <key>Value</key>
                    <string>Andalucia</string>
                    <key>Children</key>
                    <array>
                        ...
                    </array>
                </dict>
            </array>
        </dict>
    </array>
</dict>

最详细的节点是拥有以下信息的城市:

The most detailed node is the city which holds information as:

<dict>
    <key>ISO</key>
    <string>03002</string>
    <key>Value</key>
    <string>ALICANTE</string>
</dict>

然后在数据库中存储 03002 ,这是 Alicante 的唯一键.但是在我的GUI中,我显然想显示Alicante,而不是数字表示.

In the database I then store 03002 which is the unique key for Alicante in this case. But in my GUI I obviously want to display Alicante and not the numeric representation.

那么有没有一种简单的方法来搜索此plist文件以查找与此ISO匹配的值?

谢谢!

编辑

此刻,我创建了此方法.它可以解决问题,但是我不能真正称它为高效编码.所以请指教...

At the moment I created this method. It does the trick, but I cannot really call it efficient coding. So please advise...

- (NSString *)getCityNameForKey: (NSString *)ISO {
    NSString *cityName = @"";
    NSArray *citySource = [[Locations getInstance] toDataSource];

    for (int i = 0; i < [citySource count]; i++) {
        //Country
        NSDictionary *p_dictionary = [citySource objectAtIndex:i];
        NSArray *provinces = [p_dictionary objectForKey:@"Children"];
        if ([provinces count] > 0) {
            //Province
            for (int j = 0; j < [provinces count]; j++) {
                NSDictionary *r_dictionary = [provinces objectAtIndex:j];
                NSArray *regions = [r_dictionary objectForKey:@"Children"];
                if ([regions count] > 0) {
                    //City
                    for (int k = 0; k < [regions count]; k++) {
                        NSDictionary *c_dictionary = [regions objectAtIndex:k];
                        NSArray *cities = [c_dictionary objectForKey:@"Children"];
                        if ([cities count] > 0) {
                            for (int l = 0; l < [cities count]; l++) {
                                NSDictionary *result_dict = [cities objectAtIndex:l];
                                if ([[result_dict objectForKey:@"ISO"] isEqualToString:ISO]) {

                                    index_country = i;
                                    index_province = j;
                                    index_region = k;
                                    index_city = l;                                    

                                    cityName = [result_dict objectForKey:@"Value"];
                                    return cityName;
                                    break;
                                }
                            }
                        }

                    }
                }
            }
        }
    }
    return cityName;
}

推荐答案

您可以使用以下方法将plist加载到NSDictionary中:

You can load a plist into an NSDictionary with the method:

NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];

然后像访问任何词典一样简单地访问值:

Then simply access the values as you would for any dictionary:

NSDictionary *region = [dict valueForKey:@"City"];
NSString *name = [region objectForKey:@"Value"];

这篇关于如何在plist中搜索以检索特定键的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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