【发布时间】:2021-12-16 15:08:37
【问题描述】:
在尝试查找结构的大小时,内存布局的行为有点奇怪。
我可以只维护一个添加每个大小的函数。但我想知道是否有更好的方法。
enum Mode: UInt8 {
case tings
}
// this should be 5 - UInt8 + UInt16 + UInt16
struct Stuff {
let mode: Mode
let sessionID: UInt16
let sessionCount: UInt16
}
print(MemoryLayout<Stuff>.size) // 4 ???
print(MemoryLayout<UInt16>.size) // 2
print(MemoryLayout<Mode>.size) // 0 !?!?!?!?
print(MemoryLayout<Mode.RawValue>.size) // 1
【问题讨论】:
标签: swift struct memory-layout