从国家代码获取国家名称
Getting country name from country code
我已经找到 objective-c 的答案,但我在 swift 中遇到了困难。
我用它来获取当前位置的国家代码:
let countryCode = NSLocale.currentLocale().objectForKey(NSLocaleCountryCode) as! String
print(countryCode)
// printing for example US
但是如何将这个国家代码转换为国家名称,就像在这个例子中将 "US" 转换为 "United States"?
尝试做这样的事情:
// get the localized country name (in my case, it's US English)
let englishLocale = Locale.init(identifier: "en_US")
// get the current locale
let currentLocale = Locale.current
var theEnglishName : String? = englishLocale.displayName(forKey: NSLocaleIdentifier, value: currentLocale.localeIdentifier)
if let theEnglishName = theEnglishName
{
let countryName = theEnglishName.sliceFrom("(", to: ")")
print("the localized country name is \(countryName)")
}
使用这个辅助函数 :
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 找到了答案。
试试这个
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)")
正在使用 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)")
}
Swift 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
}
}
Swift 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")
超级干净的 Swift 3 版本为:
func countryName(countryCode: String) -> String? {
let current = Locale(identifier: "en_US")
return current.localizedString(forRegionCode: countryCode)
}
您可以将区域设置标识符更改为例如。 Locale.current.identifier 如果您想要本地化名称。上面的示例仅适用于英语。
Swift 4
struct CountryCode {
let country: String?
let code: String
}
let countries: [CountryCode] = NSLocale.isoCountryCodes.map {
let country = (Locale.current as NSLocale).displayName(forKey: .countryCode, value: [=10=])
return CountryCode(country: country, code: [=10=])
}
countries.map { print([=10=]) }
// Prints
// CountryCode(country: Optional("Ascension Island"), code: "AC")
// CountryCode(country: Optional("Andorra"), code: "AD")
// CountryCode(country: Optional("United Arab Emirates"), code: "AE")
// CountryCode(country: Optional("Afghanistan"), code: "AF")
这是一个紧凑的 swift 4 版本,对我有用:
func countryCode(from countryName: String) -> String? {
return NSLocale.isoCountryCodes.first { (code) -> Bool in
let name = NSLocale.current.localizedString(forRegionCode: code)
return name == countryName
}
}
或优雅的扩展,如@Memon 建议:
extension Locale {
func countryCode(from countryName: String) -> String? {
return NSLocale.isoCountryCodes.first { (code) -> Bool in
let name = self.localizedString(forRegionCode: code)
return name == countryName
}
}
}
我已经找到 objective-c 的答案,但我在 swift 中遇到了困难。
我用它来获取当前位置的国家代码:
let countryCode = NSLocale.currentLocale().objectForKey(NSLocaleCountryCode) as! String
print(countryCode)
// printing for example US
但是如何将这个国家代码转换为国家名称,就像在这个例子中将 "US" 转换为 "United States"?
尝试做这样的事情:
// get the localized country name (in my case, it's US English)
let englishLocale = Locale.init(identifier: "en_US")
// get the current locale
let currentLocale = Locale.current
var theEnglishName : String? = englishLocale.displayName(forKey: NSLocaleIdentifier, value: currentLocale.localeIdentifier)
if let theEnglishName = theEnglishName
{
let countryName = theEnglishName.sliceFrom("(", to: ")")
print("the localized country name is \(countryName)")
}
使用这个辅助函数
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 找到了答案。
试试这个
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)")
正在使用 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)")
}
Swift 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
}
}
Swift 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")
超级干净的 Swift 3 版本为:
func countryName(countryCode: String) -> String? {
let current = Locale(identifier: "en_US")
return current.localizedString(forRegionCode: countryCode)
}
您可以将区域设置标识符更改为例如。 Locale.current.identifier 如果您想要本地化名称。上面的示例仅适用于英语。
Swift 4
struct CountryCode {
let country: String?
let code: String
}
let countries: [CountryCode] = NSLocale.isoCountryCodes.map {
let country = (Locale.current as NSLocale).displayName(forKey: .countryCode, value: [=10=])
return CountryCode(country: country, code: [=10=])
}
countries.map { print([=10=]) }
// Prints
// CountryCode(country: Optional("Ascension Island"), code: "AC")
// CountryCode(country: Optional("Andorra"), code: "AD")
// CountryCode(country: Optional("United Arab Emirates"), code: "AE")
// CountryCode(country: Optional("Afghanistan"), code: "AF")
这是一个紧凑的 swift 4 版本,对我有用:
func countryCode(from countryName: String) -> String? {
return NSLocale.isoCountryCodes.first { (code) -> Bool in
let name = NSLocale.current.localizedString(forRegionCode: code)
return name == countryName
}
}
或优雅的扩展,如@Memon 建议:
extension Locale {
func countryCode(from countryName: String) -> String? {
return NSLocale.isoCountryCodes.first { (code) -> Bool in
let name = self.localizedString(forRegionCode: code)
return name == countryName
}
}
}