I'd like to access the raw bytes in Data as an array of numeric types for quick parsing. I have lots of data to parse, so I'm looking for efficiency. Data is a mix of shorts, ints, longs, and doubles.
The below code seems to work and be fast, but I'm getting deprecated warning: 'withUnsafeBytes' is deprecated
I can't figure out the updated way to treat the underlying bytes as an array of numbers for quick lookup.
var data: Data = ...
// create "virtual" arrays of different types to directly access bytes (without copying bytes)
let ushortArray: [UInt16] = data.withUnsafeBytes {
[UInt16](UnsafeBufferPointer(start: $0, count: data.count/2))
}
let intArray: [Int32] = data.withUnsafeBytes {
[Int32](UnsafeBufferPointer(start: $0, count: data.count/4))
}
// Access data simply
var i1: UInt16 = ushortArray[0]
var i2: Int32 = intArray[1]
P.S. I'm not concerned with big/little endian