swift - 二元运算符 * 不能应用于 Int 和 Double 类型的操作数

标签 swift

我正在尝试构建一个简单的 Swift 应用来计算增值税(增值税 = 20%)。

func taxesFree(number: Int) -> Double {

    var textfield = self.inputTextField.text.toInt()!
    let VAT = 0.2
    var result = textfield * VAT

    return result        
}

出于某种原因,我不断得到

Binary operator * cannot be applied to operands of type Int and Double

在线

var result = textfield * VAT    

最佳答案

你应该将一种类型转换为另一种类型,这样两个变量应该是相同的类型:

var result: Double = Double(textfield) * VAT

关于swift - 二元运算符 * 不能应用于 Int 和 Double 类型的操作数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30528748/

相关文章:

ios - 在 Swift 中重用 UIButton 的属性的最佳方法是什么

ios - 整数文字存储到 'UInt' 时溢出

ios - 如何将 array.indexOf 与协议(protocol)一起使用?

ios - Alamofire 不会在 podfile 中使用 osx 而不是 ios

ios - 从 Swift 3 中的 Dictionary<String, Any> 中获取值

ios - 检查类型并转换为具体类型以进行功能签名

ios - 如何从 Alamofire 错误中获取底层错误?

ios - 为什么 swift 协议(protocol)使用函数重载而不是不同名称的 func ?

json - 如何检测 Alamofire 请求何时为空?

ios - 自定义类未出现在 Storyboard 中