Swift Compiler error: 'Double' is not convertible to CGFloat Swift Compiler error: 'Double' is not convertible to CGFloat xcode xcode

Swift Compiler error: 'Double' is not convertible to CGFloat


You can convert it with CGFloat(M_PI).

For example the following code should work in your case (note the use of CGFloat)

let action = SKAction.rotateByAngle(CGFloat(M_PI), duration:1)


You can declare pi like this in your code : let π = CGFloat(M_PI) and then use let action = SKAction.rotateByAngle(π, duration:1)

If you are going to use π a lot, it's way more simple.

You can type π with the shortcut alt+p


The old M_PI was a Double, but the function did expect a CGFloat. A cast would be the solution.

let action = SKAction.rotateByAngle(CGFloat(M_PI), duration:1)

With respect to Swift 5 it would be now:

let action = SKAction.rotate(byAngle: .pi, duration:1)

No need to cast anymore