I get a method call back and give me the Swift "Data" struct, I need to convert it into this struct below, how can it do? struct ins_gyro_info { int64_t timestamp; //millisecond double gravity_x; double gravity_y; double gravity_z; double rotation_x; double rotation_y; double rotation_z; };
#pragma
Assuming the Data
object contains bytes laid out exactly the same as struct ins_gyro_info
in C/C++, and that structure is imported into Swift, then you can use Swift’s “unsafe” memory API to perform the conversion.
If “unsafe” is a new area for you, I recommend starting by watching Safely Manage Pointers in Swift from WWDC20.
That said, here’s one way to accomplish what you want:
let data = readRawGyroInfoDataFromCameraOrWhatever()
let gyroInfo = data.withUnsafeBytes { buffer in
buffer.load(as: ins_gyro_info.self)
}