我使用下面的代码来获取国家列表:
for (NSString *code in [NSLocale ISOCountryCodes])
{
NSString *identifier = [NSLocale localeIdentifierFromComponents:@{NSLocaleCountryCode: code}];
NSString *countryName = [[NSLocale currentLocale] displayNameForKey:NSLocaleIdentifier value:identifier];
NSLog(countryName);
}
这在iOS 7中工作得很好。但在iOS8 (beta 2)中失败- countryName
总是nil。
有人找到替代方案了吗?
我在iOS 8上也遇到过这样的问题。试着用systemLocale
代替currentLocale
let currentLocale : NSLocale = NSLocale.init(localeIdentifier : NSLocale.current.identifier)
let countryName : String? = currentLocale.displayName(forKey: NSLocale.Key.countryCode, value: countryCode)
print(countryName ?? "Invalid country code")
注意:如果手动输入localIdentifier意味着IOS 8没有列出所有国家/地区的名称(例如:"en_US")