作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
如何使 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/
我是一名优秀的程序员,十分优秀!