我正在从英语服务器中检索国家/地区名称。例如,“西类牙”
我想做的是,假设国家名称将用英文书写,获取国家代码。
我该怎么办?我发现从国家代码中获取国家名称非常容易,但我不知道如何进行相反的操作。
非常感谢。
最佳答案
杰夫的 answer在这里有所帮助,略有补充。
NSArray *countryCodes = [NSLocale ISOCountryCodes];
NSMutableArray *countries = [NSMutableArray arrayWithCapacity:[countryCodes count]];
for (NSString *countryCode in countryCodes)
{
NSString *identifier = [NSLocale localeIdentifierFromComponents: [NSDictionary dictionaryWithObject: countryCode forKey: NSLocaleCountryCode]];
NSString *country = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_UK"] displayNameForKey: NSLocaleIdentifier value: identifier];
[countries addObject: country];
}
NSDictionary *codeForCountryDictionary = [[NSDictionary alloc] initWithObjects:countryCodes forKeys:countries];
现在通过“codeForCountryDictionary”和您需要代码的国家名称,例如,
NSLog(@"%@",[codeForCountryDictionary objectForKey:@"Spain"]);
会产生“ES”的结果,这是西类牙的 2 个字母国家代码。
关于ios - 在 IOS 中从国家名称获取国家代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12671829/