2

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

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
user7222919
  • 149
  • 1
  • 3
  • 10

1 Answers1

4

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:

without explicit type conversion

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

with explicit type conversion

halfer
  • 19,824
  • 17
  • 99
  • 186
nayem
  • 7,285
  • 1
  • 33
  • 51