Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
187 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 Answer

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()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...