Search code examples
iosswiftswift2cgfloat

Binary Operator + Cannot be Applied to Operands of Type CGfloat int


I am having the same problem as earlier with a different line of code; but this time, I wasn't able to fix it with the same approach as last time:

 var Y : Int = 0
 var X : Int = 0
 @IBOutlet var ball : UIImageView!
 ball.center = CGPointMake(ball.center.x + X, ball.center.y + Y) 

This is the error I am getting:

binary operator + cannot be applied to operands of type CGfloat int


Solution

  • Declare them, instead, as the following:

    let X : CGFloat = 0.0
    let Y : CGFloat = 0.0
    

    Replying to your comment:

    The error has nothing to do with them being declared as var or let. You could declare them as var and if you so insist on declaring them as Int, you would still need to do the following:

    var X : Int = 0
    var Y : Int = 0
    ball.center = CGPointMake(view.center.x + CGFloat(X), view.center.y + CGFloat(Y))