Swift Double is Not Convertible to CGFloat
Asked Answered
I

4

28

I'm trying to draw a simple circle when I get to the following line I get the error "Double is Not Convertable to CGFloat under the startAngle = 0.0

path.addArcWithCenter(center, radius: radius, startAngle: 0.0, endAngle: Float(M_PI) * 2.0, clockwise: true)

How do I "cast" 0.0 to make it CGFloat in Swift?

The complete function I am writing:

func drawCircle() {
    // Drawing code
    var bounds:CGRect = secondView.bounds
    var center = CGPoint()
    center.x = bounds.origin.x + bounds.size.width / 2.0
    center.y = bounds.origin.y + bounds.size.height / 2.0
    var radius = (min(bounds.size.width, bounds.size.height) / 2.0)
    var path:UIBezierPath = UIBezierPath()
    path.addArcWithCenter(center, radius: radius, startAngle: CGFloat(0.0), endAngle: Float(M_PI) * 2.0, clockwise: true)
    path.stroke()    
}
Iodous answered 5/8, 2014 at 2:11 Comment(1)
Possible duplicate of Swift Compiler error: 'Double' is not convertible to CGFloatWarrenwarrener
C
29

Convert the values that need to be CGFloat to a CGFloat.

path.addArcWithCenter(center, radius: CGFloat(radius), startAngle: CGFloat(0.0), endAngle: CGFloat(M_PI) * 2.0, clockwise: true)

startAngle probably shouldn't need to be converted though if you're just passing a literal. Also note that this isn't a C style cast, but actually converting between different Swift Types.

Edit: Looking at your whole function, this works.

func drawCircle() {
        // Drawing code
        var bounds:CGRect = self.view.bounds
        var center = CGPoint()
        center.x = bounds.origin.x + bounds.size.width / 2.0
        center.y = bounds.origin.y + bounds.size.height / 2.0
        var radius = (min(bounds.size.width, bounds.size.height) / 2.0)
        var path:UIBezierPath = UIBezierPath()
        path.addArcWithCenter(center, radius: CGFloat(radius), startAngle: CGFloat(0.0), endAngle: CGFloat(Float(M_PI) * 2.0), clockwise: true)
        path.stroke()    
    }
Cointon answered 5/8, 2014 at 2:16 Comment(6)
This took care of the CGFloat error, but now I'm getting "Type does not conform to protocol FloatLiteralConvertable for the entire line. This sounds like some Swift Beta nonsense, I'm just trying to figure out how to move forward and convert these values to whatever they need to be.Iodous
is that the exact line you're using? Where do you define radius?Cointon
@user3822654 just out of curiosity, have you updated Xcode to beta 5 (which just released today). I've actually been avoiding Swift as of late due to the radical changes that have been happening with optionals, protocols, and things like CGFloats. Every time I come back, something has changed. I'm still playing with it a bit, but expect these changes until production.Singlecross
yes, i updated to the latest beta (beta 5) today. Also I added the complete function to my question above as requested by @connorIodous
@user3822654 I edited my answer to show your entire function.Cointon
Thanks, makes sense. This solved my problem, no more errors in the function. Although when I try to run drawCircle() in my swift Playground i get a really cryptic error "MyPlayground[1467] <Error>: CGContextSaveGState: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update."Iodous
S
8

You must type cast it via CGFloat(0.0). CGFloat has been adjusted to evaluate differently throughout the beta version of Xcode 6 due to the fact that in Obj-C, CGFloat casts to either a float or a double depending on the target (64 bit versus 32 bit). You must type cast a number to CGFloat in Swift to use a CGFloat as you're never guaranteed to have a float or a double (because this is dependent on the environment). This way, Swift won't throw a fit and will still be 'type' safe.

Singlecross answered 5/8, 2014 at 2:15 Comment(0)
L
2

This error will disappear in Swift 5.5.

Longs answered 18/6, 2021 at 19:48 Comment(0)
B
0

Maybe it's not a good idea, but I used NSNumber to convert Double to Float, then to CGFloat.

let myFloat = NSNumber.init(value: myDouble).floatValue
let myCGFloat = CGFloat(myFloat)
Breazeale answered 4/4, 2017 at 14:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.