Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inheritance of Encodable Class

I am writing a program using Swift 4 and Xcode 9.2. I have faced difficulties with writing encodable class (exactly class, not struct). When I am trying to inherit one class from another, JSONEncoder does not take all properties from sub class (child). Please look at this:

class BasicData: Encodable {

    let a: String
    let b: String

    init() {
        a = "a"
        b = "b"
    }
}

class AdditionalData: BasicData {

    let c: String

    init(c: String) {
        self.c = c
    }

}

let encode = AdditionalData(c: "c")

do {
    let data = try JSONEncoder().encode(encode)
    let string = String(data: data, encoding: .utf8)
    if let string = string {
        print(string)
    }
} catch {
}

It will print this: {"a":"a","b":"b"}

But I need this: {"a":"a","b":"b","c":"c"}

It look like c property of class AdditionalData just lost somewhere and somehow.

So question is: if I have class signed with protocol Encodable how to make sub class (child of this class, inherit) class properly?

I will be thankful for any help or advice.

like image 568
Alex Avatar asked Jan 13 '18 08:01

Alex


People also ask

What is encodable and decodable in Swift?

The Codable protocol in Swift is really a union of two protocols: Encodable and Decodable . These two protocols are used to indicate whether a certain struct, enum, or class, can be encoded into JSON data, or materialized from JSON data.

What does Codable mean in Swift?

Codable is the combined protocol of Swift's Decodable and Encodable protocols. Together they provide standard methods of decoding data for custom types and encoding data to be saved or transferred.

What is Codingkey Swift?

A type that can be used as a key for encoding and decoding. iOS 8.0+ iPadOS 8.0+ macOS 10.10+ Mac Catalyst 13.0+ tvOS 9.0+ watchOS 2.0+


1 Answers

Encodable and Decodable involve some code synthesis where the compiler essentially writes the code for you. When you conform BasicData to Encodable, these methods are written to the BasicData class and hence they are not aware of any additional properties defined by subclasses. You have to override the encode(to:) method in your subclass:

class AdditionalData: BasicData {
    let c: String
    let d: Int

    init(c: String, d: Int) {
        self.c = c
        self.d = d
    }

    private enum CodingKeys: String, CodingKey {
        case c, d
    }

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

See this question for a similar problem with Decodable.

like image 104
Code Different Avatar answered Sep 27 '22 20:09

Code Different