gpt4 book ai didi

ios - 如何对 Codable 的所有属性使用 decodeIfPresent?

转载 作者:搜寻专家 更新时间:2023-10-31 23:08:32 25 4
gpt4 key购买 nike

如何使 Codable 类对所有属性使用 decodeIfPresent 而无需在自定义初始化程序中键入所有属性?

一个例子:

class Book: Codable {
var name: String = "Default name"
var pages: Int = 1

required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
for key in container.allKeys {
// What to do here to call decodeIfPresent?
}
}
}

本质上,我想自己复制自动创建的初始化程序,从而改变它的工作方式。

最佳答案

显然这在此时是不可能的(Swift 4)。这是因为 Swift 不支持通过名称动态设置属性。

关于ios - 如何对 Codable 的所有属性使用 decodeIfPresent?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47126740/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com