Iphone, Obtaining a List of countries in an NSArray
Asked Answered
C

6

43

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.

Cordes answered 2/2, 2009 at 10:22 Comment(1)
This is not working with iOS 8.1 Simulator see this post :- #26613511Negligible
I
38

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.

Inanna answered 2/2, 2009 at 11:4 Comment(0)
C
94

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()
Cordes answered 2/2, 2009 at 11:9 Comment(1)
You might want to initialize sortedCountryArray with capacity of countryArray.count.Emanuel
I
38

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.

Inanna answered 2/2, 2009 at 11:4 Comment(0)
A
15

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:

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:)];
Ame answered 7/4, 2010 at 10:37 Comment(0)
R
3

Swift 3

let locale = Locale.current
let countries = Locale.isoRegionCodes.map {
    locale.localizedString(forRegionCode: $0)!
}.sorted()
Rajasthan answered 13/10, 2016 at 6:7 Comment(0)
P
0

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

let locale = NSLocale.currentLocale()
var countries = NSLocale.ISOCountryCodes().flatMap { countryCode in
    return locale.displayNameForKey(NSLocaleCountryCode, value: countryCode)
}
countries.sortInPlace()
Parashah answered 13/5, 2016 at 14:49 Comment(0)
A
-1

Got this one working on playgrounds

let locale = NSLocale(localeIdentifier: "FI")
let unsortedCountries = NSLocale.isoCountryCodes.flatMap { locale.localizedString(forCountryCode: $0) }
let sortedCountries = unsortedCountries.sorted()
Aliment answered 13/9, 2017 at 7:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.