Swift: How to use sizeof?

后端 未结 5 1627
逝去的感伤
逝去的感伤 2020-11-29 18:34

In order to integrate with C API\'s while using Swift, I need to use the sizeof function. In C, this was easy. In Swift, I am in a labyrinth of type errors.

I have

5条回答
  •  遥遥无期
    2020-11-29 18:57

    Use sizeof as follows:

    let size = sizeof(Int)
    

    sizeof uses the type as the parameter.

    If you want the size of the anInt variable you can pass the dynamicType field to sizeof.

    Like so:

    var anInt: Int = 5
    var anIntSize: Int = sizeof(anInt.dynamicType)
    

    Or more simply (pointed out by user102008):

    var anInt: Int = 5
    var anIntSize: Int = sizeofValue(anInt)
    

提交回复
热议问题