Iphone Listing Countries in NSArray

I have a menu allowing the user to select a country. Similarly, in the countrys.app menu in the address field.

Does anyone know an easy way to get a list of countries? I used NSLocale to create an array of countries, but unfortunately it only refers to country codes, not the human equivalent. I do not want GB, I want the UK.

+42
ios objective-c iphone cocoa-touch xcode
Feb 02 '09 at 10:22
source share
6 answers

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

+38
Feb 02 '09 at 11:04
source share

Thank you, patron.

If someone is interested or wants to find the same solution, this 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() 
+90
Feb 02 '09 at 11:09
source share

You might want to determine the locale ..
and too much autorealized memory, which can be critical, you never know. so create an auto-implemented pool inside a for loop. I have it:

 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:)]; 
+15
Apr 7 '10 at 10:37
source share

Swift 3

 let locale = Locale.current let countries = Locale.isoRegionCodes.map { locale.localizedString(forRegionCode: $0)! }.sorted() 
+2
Oct 13 '16 at 6:07
source share

flatMap much the same answer as above just uses flatMap in order to be shorter and faster.

 let locale = NSLocale.currentLocale() var countries = NSLocale.ISOCountryCodes().flatMap { countryCode in return locale.displayNameForKey(NSLocaleCountryCode, value: countryCode) } countries.sortInPlace() 
0
May 13 '16 at 14:49
source share

Got this one working on the playgrounds

 let locale = NSLocale(localeIdentifier: "FI") let unsortedCountries = NSLocale.isoCountryCodes.flatMap { locale.localizedString(forCountryCode: $0) } let sortedCountries = unsortedCountries.sorted() 
0
Sep 13 '17 at 7:48
source share



All Articles