如何在快速模型类上处理额外的参数

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

我是新手。我创建了一个可编码的模型类,当后端团队在其上添加了额外的参数时,我的模型类不支持。有人可以解释如何实现这一目标。

 public struct Posts : Codable {
        let`internal` : Bool?
        let documents : [Document]?
        let entityID : String?
      enum CodingKeys: String, CodingKey {
          case `internal` = "internal"
            case documents = "documents"
            case entityID = "entityID"
}
init(from decoder: Decoder) throws {
            let values = try decoder.container(keyedBy: CodingKeys.self)
            `internal` = try (values.decodeIfPresent(Bool.self, forKey: .internal) ?? false)
            documents = try values.decodeIfPresent([Document].self, forKey: .documents)
            entityID = try (values.decodeIfPresent(String.self, forKey: .entityID) ?? "")
} }


My codable look like this anything goes wrong on this.


Thanks in advance.
swift codable
1个回答
0
投票

您只需要这个,即使添加了新参数也可以。

public struct Posts: Codable {
    let`internal`: Bool?
    let documents: [Document]?
    let entityID: String?
}
© www.soinside.com 2019 - 2024. All rights reserved.