更简单的 CGRect

CGRect 与数据存储和计算更好地配合使用一直很麻烦。 一个简单的扩展解决了这个问题。

安装

  1. 将文件拖到您的 Xcode 项目中。
  2. 就是这样。

或者,如果您的内心渴望,您可以使用 Swift Package Manager。

用法

直接访问 CGRect 属性的 NSNumber 值,而无需编写重复的样板代码来转换 originsize 值。

let bounds = CGRect.init(x: 10, y: 10, width: 100, height: 300)
    
// CGRect extension lets you retrieve an NSNumber for any x, y, width, or height value
let heightForCoreData: NSNumber = bounds.number(from: .height)
    
// CGSize extension lets you directly retrieve the number value as a calculated property
let heightFromCGSizeForCoreData: NSNumber = bounds.size.heightNumber

直接访问 CGRectFloat 值,以快速执行类型安全、值有保证的计算。 同样,无需编写重复的样板代码来转换 originsize 值。

let frame = CGRect.init(x: 10, y: 10, width: 100, height: 300)
    
// CGRect extension lets you retrieve an Float for any x, y, width, or height value
let xForCalculation: Float = bounds.float(from: .x)
    
// CGPoint extension lets you directly retrieve the float value as a calculated property
let xFromCGPointForCoreData: Float = bounds.origin.xFloat