3. Swift 4.2 MemoryLayout 新方法 offset(of:)
struct Point {
var x, y: Double
}
struct Size {
var w, h: Double
var area: Double { return w*h }
}
struct Rect {
var origin: Point
var size: Size
}
MemoryLayout<Rect>.offset(of: \.origin.x) // => 0
MemoryLayout<Rect>.offset(of: \.origin.y) // => 8
MemoryLayout<Rect>.offset(of: \.size.w) // => 16
MemoryLayout<Rect>.offset(of: \.size.h) // => 24
MemoryLayout<Rect>.offset(of: \.size.area) // => nilLast updated
Was this helpful?