///带有关联值的枚举 enumTestEnum { case t1(Int, Int, Int) case t2(Int, Int) case t3(Int) case t4(Bool) } print(MemoryLayout<TestEnum>.alignment)//Int类型内存对齐:8 print(MemoryLayout<TestEnum>.size)//实际大小:24 + 1 print(MemoryLayout<TestEnum>.stride)//系统分配:24 + 8
///带有关联值的枚举 enumPassword { case num(Int, Int) case str } print(MemoryLayout<Password>.alignment)//Int类型内存对齐:8 print(MemoryLayout<Password>.size)//实际大小:16 + 1 print(MemoryLayout<Password>.stride)//系统分配:16 + 8
Struct
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
structPoint { var isHit: Bool } let p =Point(isHit: true) print(MemoryLayout<Point>.alignment)//Int类型内存对齐:1 print(MemoryLayout<Point>.size)//实际大小: 1 print(MemoryLayout<Point>.stride)//系统分配:1 print(MemoryLayout.stride(ofValue: p)) // 实例变量 p 内存是 1
structPoint { let x: Int } print(MemoryLayout<Point>.alignment)//Int类型内存对齐:8 print(MemoryLayout<Point>.size)//实际大小: 8 print(MemoryLayout<Point>.stride)//系统分配:8 let p =Point(x: 10) print(MemoryLayout.stride(ofValue: p)) // 实例变量 p 内存是 8
实际分配内存是多少?实际大小是多少? 17 24
1 2 3 4 5 6 7 8 9 10
///结构体占用内存 17,实际分配内存 16 + 8 structPoint { let x: Int let y: Int var isHit: Bool }