ios Iphone,获取 NSArray 中的国家/地区列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/502764/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 15:36:09  来源:igfitidea点击:

Iphone, Obtaining a List of countries in an NSArray

iosobjective-ciphonexcodecocoa-touch

提问by Dan Morgan

I have a menu that let's a user select a country. Exactly like that in the contacts.app country menu within the address field.

我有一个菜单,让用户选择一个国家。与地址字段中的contacts.app 国家/地区菜单中的完全一样。

Does anyone know a simple way of getting a list of countries? I have used NSLocale to generate an array of countries but it's only the country codes unfortunately and not the human readable equivalent. I don't want 'GB' I want Great Britain.

有谁知道获取国家列表的简单方法?我已经使用 NSLocale 生成了一系列国家,但不幸的是,它只是国家代码,而不是人类可读的等价物。我不想要“GB”,我想要英国。

采纳答案by Chuck

Use [[NSLocale currentLocale] displayNameForKey:NSLocaleCountryCode value:countryCode](where countryCode is an item in your list of country codes) to get the country's name in the user's current locale.

使用[[NSLocale currentLocale] displayNameForKey:NSLocaleCountryCode value:countryCode](其中 countryCode 是国家/地区代码列表中的一项)获取用户当前语言环境中的国家/地区名称。

回答by Dan Morgan

Thanks chuck.

谢谢查克。

If anyone is interested or wanted to find the same solution here is my code for a sorted array of countries.

如果有人感兴趣或想找到相同的解决方案,这里是我对国家/地区排序数组的代码。

Objective-C:

目标-C:

NSLocale *locale = [NSLocale currentLocale];
NSArray *countryArray = [NSLocale ISOCountryCodes];

NSMutableArray *sortedCountryArray = [[NSMutableArray alloc] init];

for (NSString *countryCode in countryArray) {

    NSString *displayNameString = [locale displayNameForKey:NSLocaleCountryCode value:countryCode];
    [sortedCountryArray addObject:displayNameString];

}

[sortedCountryArray sortUsingSelector:@selector(localizedCompare:)];

Swift:

迅速:

let locale = NSLocale.currentLocale()
let countryArray = NSLocale.ISOCountryCodes()
var unsortedCountryArray:[String] = []
for countryCode in countryArray {
    let displayNameString = locale.displayNameForKey(NSLocaleCountryCode, value: countryCode)
    if displayNameString != nil {
        unsortedCountryArray.append(displayNameString!)
    }
}
let sortedCountryArray = sorted(unsortedCountryArray, <)

Swift 3

斯威夫特 3

    let locale = NSLocale.current
    let unsortedCountries = NSLocale.isoCountryCodes.map { locale.localizedString(forRegionCode: 
NSMutableArray * countriesArray = [[NSMutableArray alloc] init]; 
NSLocale *locale = [[[NSLocale alloc] initWithLocaleIdentifier: @"en_US"] autorelease];

NSArray *countryArray = [NSLocale ISOCountryCodes]; 
for (NSString *countryCode in countryArray) 
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    NSString *displayNameString = [locale displayNameForKey:NSLocaleCountryCode value:countryCode];
    [countriesArray addObject:displayNameString];
    [pool release];

}

[countriesArray sortUsingSelector:@selector(compare:)];
)! } let sortedCountries = unsortedCountries.sorted()

回答by Mk.zRealDeal

You might want to define locale..
and there is too much autoreleased memory, which might be critical, ye never know. so create autoreleased pool inside the for loop as well. I've this:

您可能想要定义语言环境......
并且有太多自动释放的内存,这可能很关键,你永远不知道。所以也在 for 循环内创建自动释放池。我有这个:

let locale = Locale.current
let countries = Locale.isoRegionCodes.map {
    locale.localizedString(forRegionCode: 
let locale = NSLocale(localeIdentifier: "FI")
let unsortedCountries = NSLocale.isoCountryCodes.flatMap { locale.localizedString(forCountryCode: 
let locale = NSLocale.currentLocale()
var countries = NSLocale.ISOCountryCodes().flatMap { countryCode in
    return locale.displayNameForKey(NSLocaleCountryCode, value: countryCode)
}
countries.sortInPlace()
) } let sortedCountries = unsortedCountries.sorted()
)! }.sorted()

回答by Davuth

Swift 3

斯威夫特 3

##代码##

回答by Jo?o Nunes

Got this one working on playgrounds

让这个在操场上工作

##代码##

回答by orkoden

Pretty much the same answer as above, just uses flatMapto be shorter and swiftier.

与上面的答案几乎相同,只是使用flatMap更短更快捷。

##代码##