在Swift中使class默认符合Codable

Make class conforming to Codable by default in Swift

Swift如Codable (Decodable&Encodable) 协议的特性非常有用。 但是我发现了这样的问题: 让我们有 class Parent 符合 Codable:

class Parent: Codable {
    var name: String
    var email: String?
    var password: String?
}

好的,class 符合 Codable 协议 "from the box",你不需要写任何初始化器,它已经准备好从 JSON 像这样初始化:

{ "name": "John", "email": "johndoe@yahoo.com", "password": <null>}

但是假设我们需要其他 class,Child 继承自 Parent 并符合 Codable:

class Child: Parent {
   var token: String
   var date: Date?
}

所以 class Child 必须通过符合 Parent 来符合 Codable, 但是 class Child 的属性不会从 JSON 正确初始化。 我发现的决定是自己编写 class Child 的所有 Codable 内容,例如:

class Child: Parent {
    var token: String
    var date : Date?

    enum ChildKeys: CodingKey {
        case token, date
    }

    required init(from decoder: Decoder) throws {
        try super.init(from: decoder)
        let container = try decoder.container(keyedBy: ChildKeys.self)
        self.token = try container.decode(String.self, forKey: .token)
        self.date = try container.decodeIfPresent(Date.self, forKey: .date)
    }

    override func encode(to encoder: Encoder) throws {
        try super.encode(to: encoder)
        var container = encoder.container(keyedBy: ChildKeys.self)
        try container.encode(self.token, forKey: .token)
        try container.encodeIfPresent(self.date, forKey: .date)
    }
}

但是我感觉不太对,我是不是漏掉了什么?如何在不编写所有这些东西的情况下使 class Child 正确地符合 Codable?

这是一个很好的博客 post,其中包含对您问题的回答:source

向下滚动到继承,您将看到以下内容:

假设我们有以下 classes:

class Person : Codable {
    var name: String?
}

class Employee : Person {
    var employeeID: String?
}

我们通过从 Person class 继承来获得 Codable 一致性,但是如果我们尝试对 Employee 的实例进行编码会发生什么?

let employee = Employee()
employee.employeeID = "emp123"
employee.name = "Joe"

let encoder = JSONEncoder()
encoder.outputFormatting = .prettyPrinted
let data = try! encoder.encode(employee)
print(String(data: data, encoding: .utf8)!)

{
  "name" : "Joe"
}

这不是预期的结果,所以我们必须像这样添加自定义实现:

class Person : Codable {
    var name: String?

    private enum CodingKeys : String, CodingKey {
        case name
    }

    func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(name, forKey: .name)
    }
}

同样的子故事class:

class Employee : Person {
    var employeeID: String?

    private enum CodingKeys : String, CodingKey {
        case employeeID = "emp_id"
    }

    override func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(employeeID, forKey: .employeeID)
    }
}

结果将是:

{
  "emp_id" : "emp123"
}

这又不是预期的结果,所以这里我们通过调用 super.

使用 inheritance
// Employee.swift
    override func encode(to encoder: Encoder) throws {
        try super.encode(to: encoder)
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(employeeID, forKey: .employeeID)
    }

这终于给了我们一开始真正想要的东西:

{
    "name": "Joe",
    "emp_id": "emp123"
}

如果您对扁平化的结果不满意,这里也提供了如何避免这种情况的提示。

感谢写博客的人 post 和我的感谢。 希望对你也有帮助,干杯!