我要做的是以货币格式显示biweekPay输出,但会出现一个错误,表示无法将String类型的值转换为预期的参数类型NSNnumber。我假设行numberFormatter.stringFromNumber将允许转换,但它不允许。处理这个问题的最佳方法是什么?
let biweekPay = weekPay * 2
let numberFormatter = NSNumberFormatter()
numberFormatter.numberStyle = NSNumberFormatterStyle.CurrencyAccountingStyle
numberFormatter.stringFromNumber(biweekPayLabel.text)!
biweekPayLabel.text = "( biweekPay )"
答案 0 :(得分:2)
错误消息显示stringFromNumber
的参数 - 顾名思义 - 必须是NSNumber
个实例,而不是文本(String
)
我怀疑你的意思
let biweekPay = weekPay * 2
let numberFormatter = NSNumberFormatter()
numberFormatter.numberStyle = .CurrencyAccountingStyle
let biweekPayString = numberFormatter.stringFromNumber(biweekPay)
biweekPayLabel.text = biweekPayString ?? "n/a"