Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Casting CGFloat to Float in Swift

I need to store a value as a Float, but the source data is a CGFloat:

let myFloat : Float = myRect.origin.x 

but this results in the compiler error: 'NSNumber' is not of subtype 'Float'

So if I explicitly cast it like this:

let myFloat : Float = myRect.origin.x as Float 

but this in turn results in the compiler error: 'Cannot convert the expression's type 'Float' to 'Float''

What's the correct way to do this and satisfy the compiler please?

like image 826
Andrew Ebling Avatar asked Jun 10 '14 17:06

Andrew Ebling


1 Answers

You can use the Float() initializer:

let cgFloat: CGFloat = 3.14159 let someFloat = Float(cgFloat) 
like image 101
Erik Avatar answered Oct 21 '22 04:10

Erik