Casting CGFloat to Float in Swift

CastingSwiftCgfloat

Casting Problem Overview


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?

Casting Solutions


Solution 1 - Casting

You can use the Float() initializer:

let cgFloat: CGFloat = 3.14159
let someFloat = Float(cgFloat)

Solution 2 - Casting

If you are as lazy as I am, in an Extensions.swift define the following:

extension Int {
  var f: CGFloat { return CGFloat(self) }
}

extension Float {
  var f: CGFloat { return CGFloat(self) }
}

extension Double {
  var f: CGFloat { return CGFloat(self) }
}

extension CGFloat {
  var swf: Float { return Float(self) }
}

Then you can do:

var someCGFloatFromFloat = 1.3.f
var someCGFloatFromInt = 2.f
var someFloatFromCGFloat = someCGFloatFromFloat.swf

Solution 3 - Casting

Usually, the best solution is to keep the type and use CGFloat, even in Swift. That's because CGFloat has different size on 32bit and 64bit machines.

Keyword as can be used only for dynamic casting (for subclasses), e.g.

class A {
}

class B : A {
}

var a: A = B()
var b: B = a as B

However, Double, Int, Float etc are not subclasses of each other, therefore to "cast" you have to create a new instance, e.g.

var d: Double = 2.0
var f: Float = Float(d) //this is an initialiser call, not a cast
var i: Int = Int(d) //this is an initialiser call, not a cast

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndrew EblingView Question on Stackoverflow
Solution 1 - CastingErikView Answer on Stackoverflow
Solution 2 - CastinghyouuuView Answer on Stackoverflow
Solution 3 - CastingSulthanView Answer on Stackoverflow