How to get bytes out of an UnsafeMutableRawPointer?

load<T> reads raw bytes from memory and constructs a value of type T: let ptr = … // Unsafe[Mutable]RawPointer let i16 = ptr.load(as: UInt16.self) optionally at a byte offset: let i16 = ptr.load(fromByteOffset: 4, as: UInt16.self) There is also assumingMemoryBound() which converts from a Unsafe[Mutable]RawPointer to a Unsafe[Mutable]Pointer<T>, assuming that the pointed-to memory contains a … Read more

Swift 5.0: ‘withUnsafeBytes’ is deprecated: use `withUnsafeBytes(…)

In Swift 5 the withUnsafeBytes() method of Data calls the closure with an (untyped) UnsafeRawBufferPointer, and you can load() the value from the raw memory: let value = data.withUnsafeBytes { $0.load(as: UInt32.self) } (compare How to use Data.withUnsafeBytes in a well-defined manner? in the Swift forum). Note that this requires that the memory is aligned … Read more