xcode 6.1 iOS 8.1 NSLocale displayNameForKey NSLocaleIdentifier返回nil

Yur*_*sov 17 objective-c ios nslocale

- (NSString *)countryNameByCode:(NSString*)countryCode
{
    NSString *identifier = [NSLocale localeIdentifierFromComponents:@{NSLocaleCountryCode: countryCode}];
    NSString *countryName = [[NSLocale currentLocale] displayNameForKey:NSLocaleIdentifier value:identifier];

    return countryName;
}

这回来了nil.为什么?

Zor*_*ayr 20

这是仅适用于iOS 8.1 模拟器的已知Apple问题- 在8.1设备上无法重现.从Xcode 6.1发行说明中查看以下问题描述:

iOS 8.1模拟器中的Safari,地图和开发人员应用程序无法正确识别本地化和键盘设置(包括第三方键盘).[NSLocale currentLocale]返回en_US,只有英语和表情符号键盘可用.(18418630,18512161).

有关更多详细信息,请参阅Xcode 6.1发行说明.


小智 7

请尝试下面的代码,它应该工作.我在我的设备以及模拟器8.1上尝试过它

- (NSString *)countryNameByCode:(NSString*)countryCode {
  return [[NSLocale systemLocale] displayNameForKey:NSLocaleCountryCode value:countryCode];
}
Run Code Online (Sandbox Code Playgroud)


zyx*_*man 0

- (NSString *)countryNameByCode:(NSString*)countryCode 
{
    NSString *identifier = [[NSLocale preferredLanguages] firstObject];
    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:identifier];
    NSString *countryName = [locale displayNameForKey:NSLocaleIdentifier value:countryCode];
    return countryName;
}
Run Code Online (Sandbox Code Playgroud)

我不明白为什么[[NSLocale currentLocale] displayNameForKey...]不返回国家/地区名称,iOS 8但上面的代码应该可以解决您的问题。

  • 是的,我也没有工作......对于国家代码“US”,我得到一个标识符“_US”,这导致国家名称为零。 (2认同)