Given a variable of type Data
, how do I copy 16 bytes out of it and directly into a variable of type uuid_t
?
I'm writing some Swift code that exchanges data with an external service using Google Protocol Buffers. The service returns a data structure that contains two properties: an Int
representing a count and an Array of raw bytes Uint8
representing sequential UUIDs of 16 bytes each. In Swift, this is represented as the following struct
:
struct UUIDCollection {
var count:Int // Number of 16 byte identifiers.
var identifiers:Data // Array of bytes where every group of 16 bytes, starting at index 0, is a uuid.
}
I'm unable to figure out the correct usage of Swift pointers to allow me to do something like this:
for i in 0..<count {
let offset = Int(i * 16)
var bytes:uuid_t
let range = offset..<(offset + 16)
withUnsafeMutablePointer(to:&bytes) { (b:UnsafeMutablePointer<UInt8>) -> Void in
identifiers.copyBytes(to:b, from:range)
let uuid = UUID(uuid:bytes)
print("UUID: \(uuid.uuidString)")
}
}
The Xcode error I receive is:
Cannot convert value of type '(UnsafeMutablePointer) -> Void' to expected argument type '(UnsafeMutablePointer<_>) -> _'
What is the correct, and ideally most efficient, way of converting such an array of bytes into an array of uuid_t?
Note: The Swift code is designed to work with an existing API, which vends identifiers as a single array of bytes. Changing that API to vend a vector of identifiers or a vector of string UUIDs isn't really an option at the moment.