Swift - 具有通用字典 var 的可编码结构?

问题描述

我有一个像这样的 Swift struct

struct MyStruct: Codable {

  var id: String
  var name: String
  var createdDate: Date

}

为此,我想添加另一个属性:[String:Any] 字典。结果看起来像这样:

struct MyStruct: Codable {

  var id: String
  var name: String
  var createdDate: Date

  var attributes: [String:Any] = [:]
}

最后,我希望能够将我的 MyStruct 实例序列化为 JSON 字符串,反之亦然。但是,当我开始构建时,我收到一条错误消息,

Type 'MyStruct' does not conform to protocol 'Codable'
Type 'MyStruct' does not conform to protocol 'Decodable'

显然是 attributes var 绊倒了我的构建,但我不确定如何获得所需的结果。知道如何编码我的 struct 以支持这一点吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)