Converting CGFloat to String in Swift

前端 未结 2 1840
生来不讨喜
生来不讨喜 2021-02-05 01:46

This is my current way of converting a CGFloat to String in Swift:

let x:Float = Float(CGFloat)
let y:Int = Int(x)
let z:String = String(y)

Is

2条回答
  •  囚心锁ツ
    2021-02-05 02:23

    You can use string interpolation:

    let x: CGFloat = 0.1
    let string = "\(x)" // "0.1"
    

    Or technically, you can use the printable nature of CGFloat directly:

    let string = x.description
    

    The description property comes from it implementing the Printable protocol which is what makes string interpolation possible.

提交回复
热议问题