【发布时间】: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