Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Json 使类符合Swift中默认的可编码_Json_Swift_Inheritance_Codable - Fatal编程技术网

Json 使类符合Swift中默认的可编码

Json 使类符合Swift中默认的可编码,json,swift,inheritance,codable,Json,Swift,Inheritance,Codable,Swift的Codable(Decodable&Encodable)协议等功能非常有用。 但我发现了这样的问题: 让我们有一个符合可编码的类父: class Parent: Codable { var name: String var email: String? var password: String? } 好的,该类符合“从盒子”的可编码协议,您不需要编写任何初始值设定项,它可以从JSON进行初始化,如下所示: { "name": "John", "email":

Swift的
Codable
Decodable
&
Encodable
)协议等功能非常有用。 但我发现了这样的问题: 让我们有一个符合
可编码
的类父:

class Parent: Codable {
    var name: String
    var email: String?
    var password: String?
}
好的,该类符合“从盒子”的可编码协议,您不需要编写任何初始值设定项,它可以从JSON进行初始化,如下所示:

{ "name": "John", "email": "johndoe@yahoo.com", "password": <null>}
因此,班级儿童必须通过与家长的一致性来遵守可编程序, 但类Child的属性不能从JSON正确初始化。 我的决定是自己为班上的孩子写所有可编码的东西,比如:

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)
    }
}

但我觉得这不可能是对的,我错过了什么吗?如何让班上的孩子在不写这些东西的情况下正确地遵守Codable?

这里有一篇很好的博文,其中包括对你问题的回答:

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

假设我们有以下类别:

class Person : Codable {
    var name: String?
}

class Employee : Person {
    var employeeID: String?
}
我们通过继承Person类来获得可编码的一致性,但是如果我们尝试编码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 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来使用继承

// 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"
}
如果你对扁平化的结果不满意,这里也有一个如何避免的技巧

所有的功劳都要归功于写这篇博文的人,还有我的感谢。
希望它也能帮助你,干杯

可能重复的同意,我的坏没有检查以前的问题我的问题你到处看。在.Net中,它们使用显式的未继承属性来描述类型上的序列化…这样您就不会误以为有任何方法可以继承可序列化性。斯威夫特的方法给我的印象是一种反模式……让消费者期望一个符合类型的子类型在出生时符合。当然,从技术上来说是这样的……但正如你所确定的,它会断裂。
{
    "name": "Joe",
    "emp_id": "emp123"
}