美文网首页Swift
Binary operator '*' cannot be ap

Binary operator '*' cannot be ap

作者: survivorsfyh | 来源:发表于2023-08-16 17:09 被阅读0次

在 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)

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

相关文章

网友评论

    本文标题:Binary operator '*' cannot be ap

    本文链接:https://www.haomeiwen.com/subject/wbzkmdtx.html