iOS8-从区域设置获取国家名称
我正在使用此代码获取国家列表:iOS8-从区域设置获取国家名称,ios8,nslocale,Ios8,Nslocale,我正在使用此代码获取国家列表: for (NSString *code in [NSLocale ISOCountryCodes]) { NSString *identifier = [NSLocale localeIdentifierFromComponents:@{NSLocaleCountryCode: code}]; NSString *countryName = [[NSLocale currentLocale] displayNameForKey:NSLocaleId
for (NSString *code in [NSLocale ISOCountryCodes])
{
NSString *identifier = [NSLocale localeIdentifierFromComponents:@{NSLocaleCountryCode: code}];
NSString *countryName = [[NSLocale currentLocale] displayNameForKey:NSLocaleIdentifier value:identifier];
NSLog(countryName);
}
这在iOS 7.x中运行良好,但在iOS 8(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”)在IOS 8、9和10上仍然为零。我刚刚在IOS 10上尝试过,它不是零:顺便说一句,在IOS 10上,即使是带有currentLocale输出完整国家/地区名称的变体(阿森松岛、安道尔等)