将数据解码为结构对象失败

Failed decoding Data to a struct object

希望你一切顺利。 我在从 api 收集记录并将其存储到结构中时遇到问题。问题是如果记录的单个值丢失,解码过程会抛出错误。即整个记录都不见了。我将非常感谢听到你的意见。谢谢

下面是我的代码:

 HttpRequestHelper().GET(url: urlString, params: \["":""\], httpHeader: .application_json) { success, data in

        if success {
            do {
                let model = try JSONDecoder().decode(EmployeesModel.self, from: data!)
                
                completion(success, model, nil)
            } catch let error {
                completion(false, nil, error.localizedDescription)
            }
        } else {
            completion(false, nil, "Error: Employees Get Requests failed")
        }
    }

EmployeeModel 是:

typealias EmployeesModel = [EmployeeModel]

struct EmployeeModel: Codable {

let age: String
let id: String
let name: String
let salary: String

enum CodingKeys: String, CodingKey {

    case id
    case name = "name"
    case salary = "salary"
    case age = "age"
   }
}

尝试使用可选的。如果您使用相同的 属性 命名,则不必使用 CodingKeys。

struct EmployeeModel: Codable {

   let age: String?
   let id: String?
   let name: String?
   let salary: String?

}