我有两个问题:
let amount:String? = amountTF.text
-
amount?.characters.count <= 0
报错:
Binary operator '<=' cannot be applied to operands of type 'String.CharacterView.IndexDistance?' (aka 'Optional<Int>') and 'In
-
let am = Double(amount)
报错:
Cannot invoke initializer for type 'Double' with an argument list of type '(String?)'
我不知道怎么解决。
最佳答案
amount?.count <= 0
这里的金额是可选的。你必须确保它不是nil
.
let amount:String? = amountTF.text
if let amountValue = amount, amountValue.count <= 0 {
}
amountValue.count <= 0
只有在 amount
时才会被调用不为零。
同样的问题 let am = Double(amount)
. amount
是可选的。
if let amountValue = amount, let am = Double(amountValue) {
// am
}
关于ios - 无法使用类型为 'Double' 的参数列表调用类型为 '(String?)' 的初始值设定项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46989131/