将国家代码转换为国家/地区名称 [英] Converting Country Codes to Country Names

查看:830
本文介绍了将国家代码转换为国家/地区名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将国家/地区代码列表转换为国家/地区数组。这是我到目前为止所做的。

I need to convert a list of country codes to a country array. Here is what I have done so far.

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    pickerViewArray = [[NSMutableArray alloc] init]; //pickerViewArray is of type NSArray;
    pickerViewArray =[NSLocale ISOCountryCodes];
}


推荐答案

您可以获取一个标识符国家/地区代码 localeIdentifierFromComponents:然后获取其 displayName

You can get an identifier for a country code with localeIdentifierFromComponents: and then get its displayName.

所以要创建一个包含国家/地区名称的数组:

So to create an array with country names you can do:

NSMutableArray *countries = [NSMutableArray arrayWithCapacity: [[NSLocale ISOCountryCodes] count]];

for (NSString *countryCode in [NSLocale ISOCountryCodes])
{
    NSString *identifier = [NSLocale localeIdentifierFromComponents: [NSDictionary dictionaryWithObject: countryCode forKey: NSLocaleCountryCode]];
    NSString *country = [[NSLocale currentLocale] displayNameForKey: NSLocaleIdentifier value: identifier];
    [countries addObject: country];
}

要按字母顺序排序,您可以添加

To sort it alphabetically you can add

NSArray *sortedCountries = [countries sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];

请注意,排序后的数组是不可变的。

这篇关于将国家代码转换为国家/地区名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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