本文介绍了无法使用类型为((String?)'的参数列表调用类型为'Double'的初始化程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个问题:
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
here amount is optional. You have to make sure it not nil
.
let amount:String? = amountTF.text
if let amountValue = amount, amountValue.count <= 0 {
}
amountValue.count <= 0
仅在amount
不为零时被调用.
amountValue.count <= 0
will only be called if amount
is not nil.
与此let am = Double(amount)
相同的问题. amount
是可选的.
Same issue for this let am = Double(amount)
. amount
is optional.
if let amountValue = amount, let am = Double(amountValue) {
// am
}
这篇关于无法使用类型为((String?)'的参数列表调用类型为'Double'的初始化程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!