gpt4 book ai didi

swift - 可选的 UInt8?给出 2 字节内存大小

转载 作者:搜寻专家 更新时间:2023-10-31 22:55:55 26 4
gpt4 key购买 nike

UInt8 内存大小为 1 个字节。但是当我把它设为可选值时,它给出了 2 个字节的大小。

   var serail : UInt8? = 255
print(MemoryLayout.size(ofValue: serail)) // it gives 2 byte size.


var serail : UInt8 = 255
print(MemoryLayout.size(ofValue: serail)) // it gives 1 byte size.

如何为整数值获取恰好 1 字节的内存大小

最佳答案

在引擎盖下,一个可选的是一个枚举,看起来像这样:

enum Optional<Wrapped> {
case some(Wrapped) // not nil
case none // nil
}

符号 ?! 只是引用这个可选枚举的简写。这个额外的层导致它有 2 个字节大。

但是,如果您解包可选值,则返回值是包装值本身,因此它变为 1 个字节。

关于swift - 可选的 UInt8?给出 2 字节内存大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47130445/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com