SemAllush SemAllush - 5 months ago 21
Swift Question

How do you convert a UnsafeMutablePointer<Void> to UInt8?

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:


but it gave me the following error: Cannot find an initializer for type 'UInt8' that accepts an argument list of type 'UnsafeMutablePointer'


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)