Matt Fishkin
Matt Fishkin

Reputation: 23

Swift Float to UInt8 conversion unnecessary?

I have been getting a lot of errors about conversions in swift as of beta 4. I saw it was a pretty common problem and I was able to fix most of the errors except for this certain one:

let xDistance = testCircle.position.x - circle.position.x
let yDistance = testCircle.position.y - circle.position.y

let distance = (sqrt(pow(Float(xDistance), 2) + pow(Float(yDistance), 2)))
let minDist = testCircle.size.width * 1.5 + circle.size.width/2

if (distance >= minDist) {
    return true
}

the if (distance >= minDist) is returning the following error:

'Float' is not convertible to 'UInt8'

I'm not sure why I need to use UInt8 here, but I have tried fixing it and have just caused more conversion error. Anyone see the problem here?

Upvotes: 0

Views: 568

Answers (1)

Nate Cook
Nate Cook

Reputation: 93286

The error message doesn't make any sense, since the problem is that you're comparing a Float and a CGFloat. In previous Swift betas, CGFloat was (at least sometimes) aliased to Float, so you could get away with that, but in beta 4 it's its own type. You can just cast your float value to CGFloat in the comparison:

if (CGFloat(distance) >= minDist) {
    return true
}

It's safer to go Float -> CGFloat since on some architectures CGFloat is a Double under the hood.

Upvotes: 1

Related Questions