无法将类型 'String' 的值转换为预期的参数类型 'NSNumber'
Cannot convert value of type 'String' to expected argument type 'NSNumber'
我想做的是以货币格式显示 biweekPay 输出,但出现错误,提示无法将字符串类型的值转换为预期的参数类型 NSNnumber。我假设行 numberFormatter.stringFromNumber 允许转换,但它不允许。处理此问题的最佳方法是什么?
let biweekPay = weekPay * 2
let numberFormatter = NSNumberFormatter()
numberFormatter.numberStyle = NSNumberFormatterStyle.CurrencyAccountingStyle
numberFormatter.stringFromNumber(biweekPayLabel.text)!
biweekPayLabel.text = "( biweekPay )"
错误消息说 stringFromNumber
的参数——顾名思义——必须是一个 NSNumber
实例而不是文本 (String
)
我怀疑你是说
let biweekPay = weekPay * 2
let numberFormatter = NSNumberFormatter()
numberFormatter.numberStyle = .CurrencyAccountingStyle
let biweekPayString = numberFormatter.stringFromNumber(biweekPay)
biweekPayLabel.text = biweekPayString ?? "n/a"
我想做的是以货币格式显示 biweekPay 输出,但出现错误,提示无法将字符串类型的值转换为预期的参数类型 NSNnumber。我假设行 numberFormatter.stringFromNumber 允许转换,但它不允许。处理此问题的最佳方法是什么?
let biweekPay = weekPay * 2
let numberFormatter = NSNumberFormatter()
numberFormatter.numberStyle = NSNumberFormatterStyle.CurrencyAccountingStyle
numberFormatter.stringFromNumber(biweekPayLabel.text)!
biweekPayLabel.text = "( biweekPay )"
错误消息说 stringFromNumber
的参数——顾名思义——必须是一个 NSNumber
实例而不是文本 (String
)
我怀疑你是说
let biweekPay = weekPay * 2
let numberFormatter = NSNumberFormatter()
numberFormatter.numberStyle = .CurrencyAccountingStyle
let biweekPayString = numberFormatter.stringFromNumber(biweekPay)
biweekPayLabel.text = biweekPayString ?? "n/a"