前言
结构体,这个结构体用来表示事物的坐标点和宽高度。
public typealias NSRect = CGRect public struct CGRect { public var origin: CGPoint public var size: CGSize public init() public init(origin: CGPoint, size: CGSize) }
1、NSRect 结构体变量的创建与调用
// NSRect 结构体变量的创建与赋值 // 先定义变量,再赋值 var rect1:NSRect = NSRect() rect1.origin.x = 6; rect1.origin.y = 1; rect1.size.width = 1; rect1.size.height = 6; // Int 型值 let rect2:NSRect = NSRect(x: 1, y: 2, width: 5, height: 6) // Double 型值 let rect3:NSRect = NSRect(x: 2.1, y: 3.2, width: 6.3, height: 7.4) // CGFloat 型值 let rect4:NSRect = NSRect(x: 3.1, y: 4.2, width: 7.3, height: 8.4) // 由方法创建 let rect5:NSRect = NSMakeRect(4.1, 5.2, 6.3, 7.4) // 由方法创建 let rect6:NSRect = CGRectMake(5.1, 6.2, 7.3, 8.4) // NSRect 结构体变量值的调用 print("\(rect1.origin.x), \(rect1.origin.y), \(rect1.size.width), \(rect1.size.height)")
2、NSRect 与 String 的相互转换
let rect1:NSRect = NSMakeRect(4.1, 5.2, 6.3, 7.4) // NSRect 转 String let string:String = NSStringFromRect(rect1); // String 转 NSRect let rect:NSRect = NSRectFromString(string);