iOS:将ISO Alpha 2转换为Alpha 3国家代码
是否可以在iOS中将ISO 3166 alpha-2国家代码转换为alpha 3国家代码,例如DE转换为DEU?这里是alpha 2到alpha 3的匹配对应列表。现在你只需将其加载到一个词典中,并在你的应用程序中使用iOS:将ISO Alpha 2转换为Alpha 3国家代码,ios,country-codes,Ios,Country Codes,是否可以在iOS中将ISO 3166 alpha-2国家代码转换为alpha 3国家代码,例如DE转换为DEU?这里是alpha 2到alpha 3的匹配对应列表。现在你只需将其加载到一个词典中,并在你的应用程序中使用 plist:根据答案Franck这里是代码swift4,用于加载plist,然后将3个字母转换为国家ISO代码 普利斯特: 这个链接是一个救命稻草!如果您需要国际足联国家代码(),请小心,因为它们包含英格兰等国家。这个答案节省了我的时间。任何人在2017年发现了这个问题,请注意列
plist:根据答案Franck这里是代码swift4,用于加载plist,然后将3个字母转换为国家ISO代码 普利斯特:
这个链接是一个救命稻草!如果您需要国际足联国家代码(),请小心,因为它们包含英格兰等国家。这个答案节省了我的时间。任何人在2017年发现了这个问题,请注意列表并不完整。有些国家失踪了。
//
// CountryUtility.swift
//
import Foundation
struct CountryUtility {
static private func loadCountryListISO() -> Dictionary<String, String>? {
let pListFileURL = Bundle.main.url(forResource: "iso3166_2_to_iso3166_3", withExtension: "plist", subdirectory: "")
if let pListPath = pListFileURL?.path,
let pListData = FileManager.default.contents(atPath: pListPath) {
do {
let pListObject = try PropertyListSerialization.propertyList(from: pListData, options:PropertyListSerialization.ReadOptions(), format:nil)
guard let pListDict = pListObject as? Dictionary<String, String> else {
return nil
}
return pListDict
} catch {
print("Error reading regions plist file: \(error)")
return nil
}
}
return nil
}
/// Convertion ISO 3166-1-Alpha2 to Alpha3
/// Country code of 2 letters to 3 letters code
/// E.g: PT to PRT
static func getCountryCodeAlpha3(countryCodeAlpha2: String) -> String? {
guard let countryList = CountryUtility.loadCountryListISO() else {
return nil
}
if let countryCodeAlpha3 = countryList[countryCodeAlpha2]{
return countryCodeAlpha3
}
return nil
}
static func getLocalCountryCode() -> String?{
guard let countryCode = NSLocale.current.regionCode else { return nil }
return countryCode
}
/// This function will get full country name based on the phone Locale
/// E.g. Portugal
static func getLocalCountry() -> String?{
let countryLocale = NSLocale.current
guard let countryCode = countryLocale.regionCode else { return nil }
let country = (countryLocale as NSLocale).displayName(forKey: NSLocale.Key.countryCode, value: countryCode)
return country
}
}
if let countryCode = CountryUtility.getLocalCountryCode() {
if let alpha3 = CountryUtility.getCountryCodeAlpha3(countryCodeAlpha2: countryCode){
print(alpha3) ///result: PRT
}
}