Showing posts with label ISOCountryCodes example. Show all posts
Showing posts with label ISOCountryCodes example. Show all posts

Saturday, May 11, 2013

NSLocale ISOCountryCodes example ios


ISOCountryCodes

Returns an array of NSString objects that represents all known legal country codes.
+ (NSArray *)ISOCountryCodes
Return Value
An array of NSString objects that represents all known legal country codes.
Discussion of [NSLocale ISOCountryCodes]
Note that many of country codes do not have any supporting locale data in OS X.
Example of [NSLocale ISOCountryCodes]
NSMutableArray *countries = [NSMutableArray arrayWithCapacity: [[NSLocale ISOCountryCodes] count]];

for (NSString *countryCode in [NSLocale ISOCountryCodes])
{
    NSString *identifier = [NSLocale localeIdentifierFromComponents: [NSDictionary dictionaryWithObject: countryCode forKey: NSLocaleCountryCode]];
    NSString *country = [[NSLocale currentLocale] displayNameForKey: NSLocaleIdentifier value: identifier];
    [countries addObject: country];
}
Example of [NSLocale ISOCountryCodes]
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:)];
Example of [NSLocale ISOCountryCodes]
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:)];