Using Structs (Bytes) with SWIFT - Struct to NSData and NSData to Struct

久未见 提交于 2019-12-12 07:59:46

问题


I'm trying to understand how structs work within SWIFT. I'm familiar with how .NET handles them and how it packs bytes into a structure, etc.

I'm finding some weirdness with the following code:

struct exampleStruct {
    var ModelNumber: Byte!
    var MajorVersion: Byte!
    var MinorVersion: Byte!
    var Revision: Byte!
}


var myStruct = exampleStruct (
    ModelNumber: 1,
    MajorVersion: 2,
    MinorVersion: 3,
    Revision: 4
)

myStruct.MinorVersion  // << Returns  3

// Struct to NSData.
var data = NSData(
    bytes: &myStruct,
    length: sizeof(exampleStruct)
)

println("data: \(data)")

The println returns: "data: <01000200 03000400>"

I was expecting the data to actually be: "data: <01020304>"

Any idea why SWIFT is not packing 1 byte as 1 byte, instead it's packing a byte as 2 bytes (1 byte the value and 1 byte as 00)?

On the flip side if I do the following:

var newStruct   = exampleStruct(ModelNumber: nil, MajorVersion: nil, MinorVersion: nil, Revision: nil)

var sendBytes:[Byte] = [0x1, 0x2, 0x3, 0x4]
var newData = NSData(bytes: sendBytes, length: sendBytes.count)

newData.getBytes(
    &newStruct,
    length: sizeof(exampleStruct)
)

newStruct.MinorVersion // << Returns nil

println(newData)

The println(newData) is correct and what I expect in value: <01020304>

However, when I access the minorVersion it returns nil, not what I expected as 3.

Is there a way to force the structure to respect the packing of a byte?


回答1:


Change Byte! to Byte.

Otherwise you are creating struct holding Optional<Byte> which is larger than one byte because in addition to hold a byte, it need extra byte to indicate if it is nil

Note: This may still not working as you may need something like __attribute__((packed)) to tell compiler how to deal with alignment. AFAIK, it is not available in Swift.



来源:https://stackoverflow.com/questions/27285887/using-structs-bytes-with-swift-struct-to-nsdata-and-nsdata-to-struct

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!