1

I'm receiving what appears to be a relatively common error in my code, but examples of solutions to it on other questions didn't appear to be quite relevant to my situation. For example; (binary operator '/' cannot be applied to two 'Double' operands)

    let variable1 = Double()
    let variable2 = Double()
    let array = [5, 10]
    var variable3 = Double()

    func function() {
            let variable1 = 50 / variable2
            let variable3 = Double(arc4random_uniform(UInt32(Double(array.count))))
            let scaleAction = SKAction.scale(by: variable1 * variable3, duration: 1)

That's all the relevant code anyway. For whatever reason I receive an error, focused on the multiplication star in the last line, saying that the "Binary operator "*" cannot be applied to two "Double" operands. Why can't it? And is there a way I can fix this?

Thanks in advance.

Community
  • 1
  • 1

1 Answers1

2

The error message is a bit misleading. The real problem is that scale(by:duration:) takes a CGFloat as the scale:

open class func scale(by scale: CGFloat, duration sec: TimeInterval) -> SKAction

So, you need to pass a CGFloat. You can either work to make sure variable1 and variable3 are CGFloats, or you can use the CGFloat constructor to convert it from a Double:

let scaleAction = SKAction.scale(by: CGFloat(variable1 * variable3), duration: 1)
vacawama
  • 150,663
  • 30
  • 266
  • 294
  • Thank you! This has solved the issue entirely. – Dan Harward Jones Jan 15 '17 at 00:18
  • The question you linked was essentially the same thing. In that case, the function took an `Int` instead of `CGFloat`, but the solution was the same in that you need to make sure the two values that are being operated on by the binary operator match the type of the parameter, or you need to convert it. – vacawama Jan 15 '17 at 00:23
  • Oh I understand, I'll keep that in mind from now on. Thank you again. – Dan Harward Jones Jan 15 '17 at 01:52