【发布时间】:2020-06-23 18:35:48
【问题描述】:
我在 Swift 中有以下结构
struct SerializableRequest {
var r: Int
var m: String
var d: String
var b: [UInt8]
}
然后我创建一个结构的实例:
SerializableRequest(r: 1, m: "c", d:"l", b: [42, 24] )
并使用 CBOR 对其进行编码,得到:[164, 97, 109, 97, 99, 97, 98, 66, 42, 24, 97, 100, 97, 108, 97, 114, 1]
现在我想使用上面的字节数组再次将其解码回 Struct,但在解码时会产生输出
([SwiftCBOR.CBOR.utf8String("d"): SwiftCBOR.CBOR.utf8String("l"),
SwiftCBOR.CBOR.utf8String("r"): SwiftCBOR.CBOR.unsignedInt(1),
SwiftCBOR.CBOR.utf8String("b"): SwiftCBOR.CBOR.byteString([42, 24]),
SwiftCBOR.CBOR.utf8String("m"): SwiftCBOR.CBOR.utf8String("c")]
)
如何使用 https://github.com/myfreeweb/SwiftCBOR 使用 PATTERN MATCHING 获得 Struct
【问题讨论】: