Мне нужно преобразовать список кодов стран в массив стран. Вот что я сделал до сих пор.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
pickerViewArray = [[NSMutableArray alloc] init]; //pickerViewArray is of type NSArray;
pickerViewArray =[NSLocale ISOCountryCodes];
}
Вы можете получить идентификатор для кода страны с помощью localeIdentifierFromComponents:
, а затем получить его displayName
.
Итак, чтобы создать массив с названиями стран, вы можете сделать:
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];
}
Для сортировки по алфавиту вы можете добавить
NSArray *sortedCountries = [countries sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
. .