What is UnsafeMutablePointer? How to modify the underlying memory?

后端 未结 2 1524
梦如初夏
梦如初夏 2020-12-28 22:33

I am trying to work with SpriteKit\'s SKMutableTexture class but I don\'t know how to work with UnsafeMutablePointer< Void >. I have a vague idea that it

2条回答
  •  庸人自扰
    2020-12-28 22:55

    UnsafeMutablePointer is the Swift equivalent of void* - a pointer to anything at all. You can access the underlying memory as its memory property. Typically, if you know what the underlying type is, you'll coerce to a pointer to that type first. You can then use subscripting to reach a particular "slot" in memory.

    For example, if the data is really a sequence of UInt8 values, you could say:

    let buffer = UnsafeMutablePointer(ptr)
    

    You can now access the individual UIInt8 values as buffer[0], buffer[1], and so forth.

提交回复
热议问题