如何从货币代码获取国家/地区名称?

时间:2017-01-03 09:01:27

标签: ios objective-c swift

如何从货币代码获取国家/地区名称?这有可能吗?

3 个答案:

答案 0 :(得分:5)

替代选项,使用Swift 3 Locale类数据:

struct CurrencyToRegionMapper {

    static let locales = Locale.availableIdentifiers.map(Locale.init)

    static func locales(currencyCode: String) -> Set<Locale> {
        let localesWithCode = self.locales.filter { locale in
            locale.currencyCode == currencyCode
        }
        return Set(localesWithCode)
    }

    static func locales(currencySymbol: String) -> Set<Locale> {
        let localesWithSymbol = self.locales.filter { locale in
            locale.currencySymbol == currencySymbol
        }
        return Set(localesWithSymbol)
    }

    static func regionNames(currencyCode: String, forLocale locale: Locale = Locale.autoupdatingCurrent) -> Set<String> {
        let locale = Locale(identifier: locale.identifier) // .current and .autoupdatingCurrent doesn't work without this hack for some reason
        let localesForCode = self.locales(currencyCode: currencyCode)
        let names: [String] = localesForCode.flatMap { loc in
            if let regionCode = loc.regionCode {
                return locale.localizedString(forRegionCode: regionCode)
            } else {
                return locale.localizedString(forIdentifier: loc.identifier)
            }
        }
        return Set(names)
    }

    static func regionNames(currencySymbol: String, forLocale locale: Locale = Locale.autoupdatingCurrent) -> Set<String> {
        let locale = Locale(identifier: locale.identifier) // .current and .autoupdatingCurrent doesn't work without this hack for some reason
        let localesForSymbol = self.locales(currencySymbol: currencySymbol)
        let names: [String] = localesForSymbol.flatMap { loc in
            if let regionCode = loc.regionCode {
                return locale.localizedString(forRegionCode: regionCode)
            } else {
                return locale.localizedString(forIdentifier: loc.identifier)
            }
        }
        return Set(names)
    }

}

用法:

CurrencyToRegionMapper.locales(currencyCode: "EUR") // Returns set of locales, where currency is Euro
CurrencyToRegionMapper.locales(currencySymbol: "$") // Returns set of locales, where currency symbol is $

CurrencyToRegionMapper.regionNames(currencyCode: "GBP", forLocale: Locale(identifier: "en_POSIX")) // Returns set of regions(countries) names which use british pound in POSIX locale: ["United Kingdom", "Guernsey", "Jersey", "Isle of Man"]
CurrencyToRegionMapper.regionNames(currencySymbol: "¥", forLocale: Locale(identifier: "fi_FI")) // Returns set of regions which use currency with symbol ¥ in Finnish locale: ["Kiina", "Japani"] 

答案 1 :(得分:4)

我在this link

找到了货币代码和货币的完整列表
let codeToCountry = [
  "AED" : "United Arab Emirates",
  "AFN" : "Afghanistan",
  "ALL" : "Albania",
  "AMD" : "Armenia",
  "ANG" : "Netherlands Antilles",
  "AOA" : "Angola",
  "ARS" : "Argentina",
  "AUD" : "Australia, Australian Antarctic Territory, Christmas Island, Cocos (Keeling) Islands, Heard and McDonald Islands, Kiribati, Nauru, Norfolk Island, Tuvalu",
  "AWG" : "Aruba",
  "AZN" : "Azerbaijan",
  "BAM" : "Bosnia and Herzegovina",
  "BBD" : "Barbados",
  "BDT" : "Bangladesh",
  "BGN" : "Bulgaria",
  "BHD" : "Bahrain",
  "BIF" : "Burundi",
  "BMD" : "Bermuda",
  "BND" : "Brunei",
  "BOB" : "Bolivia",
  "BOV" : "Bolivia",
  "BRL" : "Brazil",
  "BSD" : "Bahamas",
  "BTN" : "Bhutan",
  "BWP" : "Botswana",
  "BYR" : "Belarus",
  "BZD" : "Belize",
  "CAD" : "Canada",
  "CDF" : "Democratic Republic of Congo",
  "CHE" : "Switzerland",
  "CHF" : "Switzerland, Liechtenstein",
  "CHW" : "Switzerland",
  "CLF" : "Chile",
  "CLP" : "Chile",
  "CNY" : "Mainland China",
  "COP" : "Colombia",
  "COU" : "Colombia",
  "CRC" : "Costa Rica",
  "CUP" : "Cuba",
  "CVE" : "Cape Verde",
  "CYP" : "Cyprus",
  "CZK" : "Czech Republic",
  "DJF" : "Djibouti",
  "DKK" : "Denmark, Faroe Islands, Greenland",
  "DOP" : "Dominican Republic",
  "DZD" : "Algeria",
  "EEK" : "Estonia",
  "EGP" : "Egypt",
  "ERN" : "Eritrea",
  "ETB" : "Ethiopia",
  "EUR" : "European Union, see eurozone",
  "FJD" : "Fiji",
  "FKP" : "Falkland Islands",
  "GBP" : "United Kingdom",
  "GEL" : "Georgia",
  "GHS" : "Ghana",
  "GIP" : "Gibraltar",
  "GMD" : "Gambia",
  "GNF" : "Guinea",
  "GTQ" : "Guatemala",
  "GYD" : "Guyana",
  "HKD" : "Hong Kong Special Administrative Region",
  "HNL" : "Honduras",
  "HRK" : "Croatia",
  "HTG" : "Haiti",
  "HUF" : "Hungary",
  "IDR" : "Indonesia",
  "ILS" : "Israel",
  "INR" : "Bhutan, India",
  "IQD" : "Iraq",
  "IRR" : "Iran",
  "ISK" : "Iceland",
  "JMD" : "Jamaica",
  "JOD" : "Jordan",
  "JPY" : "Japan",
  "KES" : "Kenya",
  "KGS" : "Kyrgyzstan",
  "KHR" : "Cambodia",
  "KMF" : "Comoros",
  "KPW" : "North Korea",
  "KRW" : "South Korea",
  "KWD" : "Kuwait",
  "KYD" : "Cayman Islands",
  "KZT" : "Kazakhstan",
  "LAK" : "Laos",
  "LBP" : "Lebanon",
  "LKR" : "Sri Lanka",
  "LRD" : "Liberia",
  "LSL" : "Lesotho",
  "LTL" : "Lithuania",
  "LVL" : "Latvia",
  "LYD" : "Libya",
  "MAD" : "Morocco, Western Sahara",
  "MDL" : "Moldova",
  "MGA" : "Madagascar",
  "MKD" : "Former Yugoslav Republic of Macedonia",
  "MMK" : "Myanmar",
  "MNT" : "Mongolia",
  "MOP" : "Macau Special Administrative Region",
  "MRO" : "Mauritania",
  "MTL" : "Malta",
  "MUR" : "Mauritius",
  "MVR" : "Maldives",
  "MWK" : "Malawi",
  "MXN" : "Mexico",
  "MXV" : "Mexico",
  "MYR" : "Malaysia",
  "MZN" : "Mozambique",
  "NAD" : "Namibia",
  "NGN" : "Nigeria",
  "NIO" : "Nicaragua",
  "NOK" : "Norway",
  "NPR" : "Nepal",
  "NZD" : "Cook Islands, New Zealand, Niue, Pitcairn, Tokelau",
  "OMR" : "Oman",
  "PAB" : "Panama",
  "PEN" : "Peru",
  "PGK" : "Papua New Guinea",
  "PHP" : "Philippines",
  "PKR" : "Pakistan",
  "PLN" : "Poland",
  "PYG" : "Paraguay",
  "QAR" : "Qatar",
  "RON" : "Romania",
  "RSD" : "Serbia",
  "RUB" : "Russia, Abkhazia, South Ossetia",
  "RWF" : "Rwanda",
  "SAR" : "Saudi Arabia",
  "SBD" : "Solomon Islands",
  "SCR" : "Seychelles",
  "SDG" : "Sudan",
  "SEK" : "Sweden",
  "SGD" : "Singapore",
  "SHP" : "Saint Helena",
  "SKK" : "Slovakia",
  "SLL" : "Sierra Leone",
  "SOS" : "Somalia",
  "SRD" : "Suriname",
  "STD" : "São Tomé and Príncipe",
  "SYP" : "Syria",
  "SZL" : "Swaziland",
  "THB" : "Thailand",
  "TJS" : "Tajikistan",
  "TMM" : "Turkmenistan",
  "TND" : "Tunisia",
  "TOP" : "Tonga",
  "TRY" : "Turkey",
  "TTD" : "Trinidad and Tobago",
  "TWD" : "Taiwan and other islands that are under the effective control of the Republic of China (ROC)",
  "TZS" : "Tanzania",
  "UAH" : "Ukraine",
  "UGX" : "Uganda",
  "USD" : "American Samoa, British Indian Ocean Territory, Ecuador, El Salvador, Guam, Haiti, Marshall Islands, Micronesia, Northern Mariana Islands, Palau, Panama, Puerto Rico, East Timor, Turks and Caicos Islands, United States, Virgin Islands",
  "USN" : "United States",
  "USS" : "United States",
  "UYU" : "Uruguay",
  "UZS" : "Uzbekistan",
  "VEB" : "Venezuela",
  "VND" : "Vietnam",
  "VUV" : "Vanuatu",
  "WST" : "Samoa",
  "XAF" : "Cameroon, Central African Republic, Congo, Chad, Equatorial Guinea, Gabon",
  "XAG" : "",
  "XAU" : "",
  "XBA" : "",
  "XBB" : "",
  "XBC" : "",
  "XBD" : "",
  "XCD" : "Anguilla, Antigua and Barbuda, Dominica, Grenada, Montserrat, Saint Kitts and Nevis, Saint Lucia, Saint Vincent and the Grenadines",
  "XDR" : "International Monetary Fund",
  "XFO" : "Bank for International Settlements",
  "XFU" : "International Union of Railways",
  "XOF" : "Benin, Burkina Faso, Côte d'Ivoire, Guinea-Bissau, Mali, Niger, Senegal, Togo",
  "XPD" : "",
  "XPF" : "French Polynesia, New Caledonia, Wallis and Futuna",
  "XPT" : "",
  "XTS" : "",
  "XXX" : "",
  "YER" : "Yemen",
  "ZAR" : "South Africa",
  "ZMK" : "Zambia",
  "ZWD" : "Zimbabwe"]

和一些解析dict的函数:

func countryBy(code : String) -> String? {
    return codeToCountry[code]
}

countryBy(code: "ZWD")
countryBy(code: "")
countryBy(code: "asda")

这是一些有效的例子:

https://iswift.org/playground?blvcE5

答案 2 :(得分:0)

该国家/地区不是每种货币唯一。另一种解决方案是先通过国家/地区查找货币(例如,使用this),然后按货币分组,并使用该货币的国家/地区列表。最后,您可以为具有多个国家/地区的这些货币选择一个代表国家/地区。

假设以国家和货币作为其列的pandas DataFrame,则可以通过以下方式进行分组:

df.groupby('currency').country.apply(list).reset_index()