本文介绍了如何减少您从NSLocale.Key.currencyCode返回的货币代码的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ios应用程序,试图让用户选择他们要使用的货币.现在,我有完整的货币清单,但那里似乎有些重复,例如:

I have an ios app that I'm trying to allow the users to select which currency they want to use. Right now I have the full list of currencies but there seem to be some duplicates there such as:

有没有一种方法可以过滤掉其他的?美元并不是唯一带有倍数的美元,有些美元列出了日期范围.

Is there a way to filter out the others? The dollar isn't the only one with multiples some have date ranges listed with them.

我确定有一些内置方法可以做到这一点,但是到目前为止,我的搜索还没有为我指明正确的方向.

I'm sure there is some built-in method that does this, but my searching so far hasn't pointed me in the right direction.

这是我在做什么:

let locale = NSLocale.current as NSLocale
let currencyCodesArray = NSLocale.isoCurrencyCodes
var currencies = [CurrencyModel]()

for currencyCode in currencyCodesArray {
        let currencyName = locale.displayName(forKey:

            NSLocale.Key.currencyCode, value : currencyCode)
        let currencySymbol = locale.displayName(forKey:NSLocale.Key.currencySymbol, value : currencyCode)

        if let _ = currencySymbol, let currencyName = currencyName {
            let currencyModel = CurrencyModel()
            currencyModel.currencyName = currencyName
            currencyModel.currencyCode = currencyCode

            currencies.append(currencyModel)
        }
    }

然后在talbeView中使用该数据

And then using that data in a talbeView

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "cell")! as! CurrencyTableViewCell

    cell.name.text = currencies[indexPath.row].currencyName
    cell.symbol.text = currencies[indexPath.row].currencyCode

    return cell
}

这是我的货币模型

class CurrencyModel {
var currencyName = ""
var currencyCode = ""

}

推荐答案

您应该使用

Locale.commonISOCurrencyCodes

而不是

Locale.isoCurrencyCodes

这篇关于如何减少您从NSLocale.Key.currencyCode返回的货币代码的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-24 05:04