Binary operator ‘*‘ cannot be applied to two ‘Double?‘ operands

在 swift 中声明 Double 类型参数变量在进行运算处理时抛出了如下异常

Binary operator '*' cannot be applied to two 'Double?' operands

情况一

参数类型不匹配,需将参数类型进行匹配

self.max = height / (length * width) // 初始
self.max = height / (length * Double(width)) // 改后

情况二

初始定义参数的变量可能为空,添加置空条件,声明参数末尾添加 "!" 字符

let length = Double(self.lengthText)!
let width = Double(self.weightText)!
let height = Double(self.heightText)!
self.max = height / (length * width)

以上便是此次分享的全部内容,希望能对大家有所帮助!

猜你喜欢

转载自blog.csdn.net/survivorsfyh/article/details/132345378