我有两个问题:
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。
amount?.count <= 0
nil
let amount:String? = amountTF.text if let amountValue = amount, amountValue.count <= 0 { }
amountValue.count <= 0仅在amount不为nil时被调用。
amountValue.count <= 0
amount
同样的问题let am = Double(amount)。amount是可选的。
if let amountValue = amount, let am = Double(amountValue) { // am }