2016-02-28 214 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是否需要本地化名稱。以上示例僅適用於英語。

相關問題