Binary operator '*' cannot be applied to operands of type 'int' and 'double [duplicate]

邮差的信 提交于 2019-12-24 02:21:40

问题


I keep getting this binary operator error that "Binary operator '*' cannot be applied to operands of type 'int' and 'double'

var listCount = imageNameList.count
var roll: Double = SUPCalculator.degrees(motion.attitude.roll)
if startDegree == 0.0 {
    self.startDegree = SUPCalculator.degrees(motion.attitude.roll)
}
var diff: Double = roll - startDegree
var diffChange: Double = diff / 50
// I get the error here 
var diffIndex = listCount * diffChange

回答1:


Swift is strongly typed and doesn't coerce implicitly. So you need an explicit type conversion:

var diffIndex: Double = Double(listCount) * diffChange

This is different from casting because Int is not a subclass of Double. What you are doing is asking for a completely new Double value to be built at runtime.

This is without type conversion. Here you get the error. See at the console. It says,

overloads for '*' exist with these partially matching parameter lists: (Int, Int), (Double, Double)

So either you make both of them Int or both of them Double:

This is from explicit conversion. See, here no error remains:



来源:https://stackoverflow.com/questions/41540039/binary-operator-cannot-be-applied-to-operands-of-type-int-and-double

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!