在學(xué)習(xí)Swift 3的過程中整理了一些筆記,如果想看其他相關(guān)文章可前往《Swift 3必看》系列目錄
<code>sizeof</code>這個方法名直接取自C語言,但是實(shí)際上LLVM中并沒有一個函數(shù)叫<code>sizeof</code>。而且<code>sizeof</code>的使用范圍很窄,不像<code>map</code>,<code>filter</code>這種經(jīng)常會全局用到。所以重新定義了一個結(jié)構(gòu)體<code> MemoryLayout </code>來實(shí)現(xiàn)原來<code>sizeof</code>的功能,使用上也有變化。
主要有兩種方式,一種是直接通過泛型參數(shù)從靜態(tài)變量獲?。?/p>
let stringSize = MemoryLayout<String>.size
也可以通過調(diào)用靜態(tài)方法獲取
let stringValue = "A"
let varSize = MemoryLayout.size(ofValue: stringValue)
兩者都會得到正確的結(jié)果:

除了<code>sizeof</code>, MemoryLayout還可以獲取<code>stride</code>,<code> alignment </code>。
歡迎關(guān)注我的微博:@沒故事的卓同學(xué)
相關(guān)鏈接:
SE-0101-Reconfiguring sizeof and related functions into a unifiedMemoryLayout struct