Swift 5中其他种类的Codable?(非JSON编码者)

问题描述 投票:0回答:1

AppleSwift 5是否提供了其他 Codable 序列化的实现,开箱即用。JSONEncoderJSONDecoder?

我知道我可以自己写,但是我想对于某些东西序列化到存储,虽然JSONEncoder很方便,因为它是人类可读的,我可能可以通过序列化我的二进制对象来压缩它。

每次我在网上搜索 Codable 协议,每一篇文章都只谈JSON,好像这是唯一的序列化方式。

swift codable
1个回答
0
投票

当然,有 PropertyListEncoder 它基本上是xml。

struct SomeStruct: Codable { let someProperty: String }

try? PropertyListEncoder().encode(SomeStruct(someProperty: "someValue"))
try? PropertyListDecoder().decode(SomeStruct.self, from: data)
© www.soinside.com 2019 - 2024. All rights reserved.