How do you convert a UnsafeMutablePointer to UInt8?

前端 未结 2 510
一个人的身影
一个人的身影 2021-01-17 20:58

The question is really simple, but somehow I don\'t know how to do it:

How do you convert a UnsafeMutablePointer to UInt8?

I tried converting it like this:

相关标签:
2条回答
  • 2021-01-17 21:16

    Use the pointee property in the latest Swift.

    0 讨论(0)
  • 2021-01-17 21:38

    You have to convert the pointer to the correct type first (a pointer to UInt8) and then you can access the memory it points to:

    let u8 = UnsafePointer<UInt8>(theUnsafeMutablePointerVar).memory
    

    In Swift 3, a void pointer from C is imported to Swift as UnsafeMutableRawPointer, and one can read the pointed-to data with

    let u8 = theUnsafeMutablePointerVar.load(as: UInt8.self)
    
    0 讨论(0)
提交回复
热议问题