在以下代码中,testP.deviceID
和testP.deviceID1
应具有相同的值。但是testP.deviceID1包含2(它应该包含513)。所以有一些基本的东西,我不理解对齐?
let byteArray:[UInt8] = [ 0x01,0x02,23,24,25,0x01,0x02 ]
let testData = Data(bytes: byteArray )
struct testStruct {
let deviceID :UInt16 //0..1
let byte0 :UInt8
let byte1 :UInt8
let byte2 :UInt8
let deviceID1 :UInt16 //5..6
}
let testP = testData.withUnsafeBytes { (ptr: UnsafePointer<testStruct>) -> testStruct in
return ptr.pointee
}
print("\(testP.deviceID):\(testP.byte0):\(testP.deviceID1)")