How to reduce the amount of currency codes you get back from NSLocale.Key.currencyCode

时光怂恿深爱的人放手 提交于 2019-12-25 18:41:36

问题


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.

Here is what I am doing:

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)
        }
    }

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
}

And this is my currency model

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

}


回答1:


You should be using

Locale.commonISOCurrencyCodes

rather than

Locale.isoCurrencyCodes



回答2:


If they all have that form: ie bit you want (bit you don't want) you could search for regular expressions. Search the list to find the shortest ones and keep those. You'd then have to do something about other countries which use dollars, or other currencies etc.



来源:https://stackoverflow.com/questions/52278097/how-to-reduce-the-amount-of-currency-codes-you-get-back-from-nslocale-key-curren

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!