2016-02-28 200 views
4

我已经找到了针对objective-c的这个答案,但是我很难很快地做到这一点。从国家代码中获取国家/地区名称

我已经使用这个来获取当前位置的国家代码:

 let countryCode = NSLocale.currentLocale().objectForKey(NSLocaleCountryCode) as! String 
    print(countryCode) 
// printing for example US 

但我怎么在这个例子中转换这个国家代码,国家名称,如将“美”到“美国“?

回答

4

尝试做这样的事情:

// get the localized country name (in my case, it's US English) 
let englishLocale : NSLocale = NSLocale.init(localeIdentifier : "en_US") 

// get the current locale 
let currentLocale = NSLocale.currentLocale() 

var theEnglishName : String? = englishLocale.displayNameForKey(NSLocaleIdentifier, value: currentLocale.localeIdentifier) 
if let theEnglishName = theEnglishName 
{ 
    let countryName = theEnglishName.sliceFrom("(", to: ")") 
    print("the localized country name is \(countryName)") 
} 

与这个辅助功能that I found here

import Foundation 

extension String { 
    func sliceFrom(start: String, to: String) -> String? { 
     return (rangeOfString(start)?.endIndex).flatMap { sInd in 
      (rangeOfString(to, range: sInd..<endIndex)?.startIndex).map { eInd in 
       substringWithRange(sInd..<eInd) 
      } 
     } 
    } 
} 

我想通了这一点通过研究为this related question

0

试试这个

let countryLocale : NSLocale = NSLocale.currentLocale() 
      let countryCode = countryLocale.objectForKey(NSLocaleCountryCode)// as! String 
      let country = countryLocale.displayNameForKey(NSLocaleCountryCode, value: countryCode!) 
      print("Country Locale:\(countryLocale) Code:\(countryCode) Name:\(country)") 
0

在应用程序委托上Swift3

import Foundation 

    extension String { 
     func sliceFrom(start: String, to: String) -> String? { 
      return (range(of: start)?.upperBound).flatMap({ (sInd) -> String? in 
        (range(of: to, range: sInd..<endIndex)?.lowerBound).map { eInd in 
         substring(with: sInd..<eInd) 
     } 
    }) 
    } 
    } 

使用工作

 let englishLocale : NSLocale = NSLocale.init(localeIdentifier : "en_US") 

    // get the current locale 
    let currentLocale = NSLocale.current 

    var theEnglishName : String? = englishLocale.displayName(forKey: NSLocale.Key.identifier, value: currentLocale.identifier) 
    if let theEnglishName = theEnglishName 
    { 
     countryName = theEnglishName.sliceFrom(start: "(", to: ")") 
     print("the localized country name is \(countryName)") 
    } 
12

斯威夫特3

func countryName(from countryCode: String) -> String { 
    if let name = (Locale.current as NSLocale).displayName(forKey: .countryCode, value: countryCode) { 
     // Country name was found 
     return name 
    } else { 
     // Country name cannot be found 
     return countryCode 
    } 
} 
0

斯威夫特3

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”)

+0

一个真正的Swift 3答案会使用Locale而不是NSLocale。 – Moritz

+0

是的,我同意,但是,对于使用NSLocale的IOS 8,如果您使用的是Locale,则意味着IOS 8不会返回正确的响应。 –

2

超级干净斯威夫特3版本将是:

func countryName(countryCode: String) -> String? { 
    let current = Locale(identifier: "en_US") 
    return current.localizedString(forRegionCode: countryCode) ?? nil 
} 

您可以更改区域标识符为例如。 Locale.current.identifier是否需要本地化名称。以上示例仅适用于英语。

相关问题