xcode 6.1 iOS 8.1 NSLocale displayNameForKey NSLocaleIdentifier return nil

后端 未结 5 2137
旧时难觅i
旧时难觅i 2021-02-12 20:52
- (NSString *)countryNameByCode:(NSString*)countryCode
{
    NSString *identifier = [NSLocale localeIdentifierFromComponents:@{NSLocaleCountryCode: countryCode}];
    NSStrin         


        
5条回答
  •  抹茶落季
    2021-02-12 21:19

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

    I don't understand why [[NSLocale currentLocale] displayNameForKey...] doesn't return country name in iOS 8 but the code above should resolve your problem.

提交回复
热议问题