问题
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