string - 在 Swift 中将 CGFloat 转换为字符串

标签 string swift cgfloat

这是我目前在 Swift 中将 CGFloat 转换为 String 的方法:

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

有没有更有效的方法?

最佳答案

您可以使用 string interpolation :

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

或者从技术上讲,您可以直接使用 CGFloat 的可打印特性:

let string = x.description

description 属性来自于它实现了 Printable 协议(protocol),这使得字符串插值成为可能。

关于string - 在 Swift 中将 CGFloat 转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25154087/

相关文章:

java - JDO @ DataNucleus 中的无限字符串? (H2 数据库)

php - 如何找到相似的产品

ios - 关闭的执行顺序

Swift:检查 NSArray 中的哪个值最接近另一个给定值

ios - 从 anchor 约束读取 CGFloat

c++ - 将包含数字的字符串解析为整数数组

java - Avro 和 java : deserialized map of string doesn't "equals" original map

ios - 当我点击 MapKit 中的图钉时如何执行操作? ios9, swift 2

swift - 更改 TextField 中的清除按钮

iphone - 隐式转换将 64 位缩短为 32 位