如何减少从NSLocale.Key.currencyCode返回的货币代码的数量

时间:2018-09-11 14:20:29

标签: swift nslocale currency-formatting

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

enter image description here

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

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

这是我在做什么:

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

}

2 个答案:

答案 0 :(得分:1)

您应该使用

Locale.commonISOCurrencyCodes

而不是

Locale.isoCurrencyCodes

答案 1 :(得分:-3)

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