Skip to content

Instantly share code, notes, and snippets.

@nonstriater
Last active August 29, 2015 14:00
Show Gist options
  • Save nonstriater/fa4dcb03b5b8087c53e9 to your computer and use it in GitHub Desktop.
Save nonstriater/fa4dcb03b5b8087c53e9 to your computer and use it in GitHub Desktop.
NSLocale中取出国家country信息并按照国家名字排序
//NSLocale中取出国家country信息并按照国家名字排序
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en"];
NSArray *countryCodes = [NSLocale ISOCountryCodes];// 所有国家编码
NSMutableArray *countriesUnsorted = [[NSMutableArray alloc] initWithCapacity:countryCodes.count];
for (NSString *countryCode in countryCodes) {
NSString *displayNameString = [locale displayNameForKey:NSLocaleCountryCode value:countryCode];// 获得编码对应的国家名字
NSDictionary *cd = @{@"name": displayNameString, @"code":countryCode};
[countriesUnsorted addObject:cd];
}
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];//排序方式
NSArray *sortDescriptors = @[sortDescriptor];
_regionArray = [countriesUnsorted sortedArrayUsingDescriptors:sortDescriptors];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment