Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
184 views
in Technique[技术] by (71.8m points)

ios - Iphone, Obtaining a List of countries in an NSArray

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.

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

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

    let locale = NSLocale.current
    let unsortedCountries = NSLocale.isoCountryCodes.map { locale.localizedString(forRegionCode: $0)! }
    let sortedCountries = unsortedCountries.sorted()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...