[英]swift MemoryLayout for class and struct
I am investigating MemoryLayout
and I came across this:我正在调查
MemoryLayout
,我遇到了这个:
struct A {
var name:String
}
class B {
var name:String
init(name: String) {
self.name = name
}
}
let a = A(name: "a")
let sizeA = MemoryLayout.size(ofValue: a)
print("struct size: " + "\(sizeA)") // 16
let b = B(name: "B")
let sizeB = MemoryLayout.size(ofValue: b)
print("class size: " + "\(sizeB)"). // 8
Can you please explain to me why the size is different?你能解释一下为什么尺寸不同吗? Thanks
谢谢
When using size(ofValue:)
for a class
, the returned value is the size of the pointer of that class.当对
class
使用size(ofValue:)
时,返回值是该 class 指针的大小。 However, when dealing with a struct it returns the size of that instance hence the diffrence.但是,在处理结构时,它会返回该实例的大小,从而返回差异。
More info here: When is my struct too large?更多信息在这里: 我的结构什么时候太大了?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.