【问题标题】:How to reduce the amount of currency codes you get back from NSLocale.Key.currencyCode如何减少从 NSLocale.Key.currencyCode 返回的货币代码数量
【发布时间】:2018-09-11 14:20:29
【问题描述】:

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

有没有办法过滤掉其他人?美元不是唯一具有倍数的美元,有些还列出了日期范围。

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

这是我正在做的事情:

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 中使用该数据

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 = ""

}

【问题讨论】:

  • 请说明您进行了哪些研究,您已经尝试过哪些内容,哪些无效,代码示例等。阅读minimal reproducible example,然后阅读edit您的问题,显示演示问题的最少代码
  • currencyCodesArray 来自哪里?请提供更多背景信息。

标签: swift nslocale currency-formatting


【解决方案1】:

你应该使用

Locale.commonISOCurrencyCodes

而不是

Locale.isoCurrencyCodes

【讨论】:

    【解决方案2】:

    如果它们都具有这种形式:即您想要的位(您不想要的位),您可以搜索正则表达式。搜索列表以找到最短的并保留它们。然后,您必须对使用美元或其他货币等的其他国家/地区做点什么。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-07
      • 2012-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-08
      • 1970-01-01
      相关资源
      最近更新 更多