views:

1450

answers:

4

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.

Any ideas?

Thanks

+3  A: 

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.

Chuck
+4  A: 

Thanks chuck.

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

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(compare:)];
Dan Morgan
+2  A: 

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];
    [countriesDict setObject:countryCode forKey: displayNameString];

    [pool release];

}

[countriesArray sortUsingSelector:@selector(compare:)];
Mk.zRealDeal
A: 

Can I get information of the continent? I need to group the country according to the continent. Thanks