这段代码是用于通过国家/地区码获取对应的国家/地区名称,但是如果传入的国家/地区码无效,就会返回
nil
。因此,可能需要检查一下传入的国家/地区码是否正确。nil
。因此,可能需要检查一下传入的国家/地区码是否正确。这是一个针对iOS 8.1模拟器的已知Apple问题,而在8.1设备上不能重现。请参见Xcode 6.1版本说明中以下问题描述:
iOS 8.1模拟器在Safari、地图和开发人员应用程序中无法正确遵守本地化和键盘设置(包括第三方键盘)。[NSLocale currentLocale] 返回 en_US,并且只有英语和表情键盘可用。(18418630, 18512161)。
欲了解更多详情,请查看Xcode 6.1版本说明。
- (NSString *)countryNameByCode:(NSString*)countryCode {
return [[NSLocale systemLocale] displayNameForKey:NSLocaleCountryCode value:countryCode];
}
NSLocale *currentLocale = [[NSLocale alloc] initWithLocaleIdentifier:[NSLocale currentLocale].localeIdentifier];
for (AVSpeechSynthesisVoice *voice in [AVSpeechSynthesisVoice speechVoices]) {
NSString *languageLocalised = [currentLocale displayNameForKey:NSLocaleIdentifier value:voice.language];
NSLog(@"%@ displayNameForKey %@: %@", currentLocale.localeIdentifier, voice.language, languageLocalised);
}
NSString *language = [[NSLocale preferredLanguages] objectAtIndex: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;
}
我不明白为什么在iOS 8
中[[NSLocale currentLocale] displayNameForKey...]
不能返回国家名称,但是上面的代码应该可以解决你的问题。
NSLocaleCountryCode
而不是 NSLocaleIdentifier
。 - HiveHicks