swift - Codable 类中的 Codable 变量

标签 swift codable

给定这个类:

class MyClass: Codable {
    var variable : Codable? = nil
}

我得到错误:

Type 'MyClass' does not conform to protocol 'Decodable'

Type 'MyClass' does not conform to protocol 'Encodable'

如何将符合 Codable 的通用变量作为 Codable 类中的属性?

最佳答案

如果我答对了你的问题可能是这样

class MyClass<T: Codable>: Codable {
    var variable : T? = nil
}

关于swift - Codable 类中的 Codable 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50837470/

相关文章:

ios - 如何在 Swift 中将类分配给启动画面?

swift - 有没有办法更简洁地使用 guard 语句?

ios - 点击发送或取消按钮后如何关闭邮件 View Controller

ios - Swift 4 可解码 : The given data was not valid JSON

ios - 类型 '[Root]' 的值没有成员 'commit' 。我如何解码 JSON 根数组

json - 无法通过可编码的swift从json中获取数据

ios - Swift 可编码,当 JSON 中缺少键时,类属性的默认值

arrays - UInt8 数组到 Swift 中的字符串

swift - SceneKit AR 游戏 fps 越来越低,设备随着使用而变热

ios - JSON 解析,嵌套 JSON 结构问题