使用“Codable”设置属性值不适用于继承

时间:2018-04-29 20:34:22

标签: swift codable

我无法在子类中设置b属性。它是继承自Codable的父类,并且似乎运行良好。

我觉得我错过了一些非常明显的东西,但我很难看到木头的树木。

下面是我的问题的游乐场示例。 b仍为0,尽管设置为10。它是传入的子类,但是可以设置的父属性(非常奇怪!)。

class Primary : Codable {
    var a: Int = 0
}

class Secondary : Primary {
    var b: Int = 0
}

let c = Secondary.self

func testRef<T: Codable>(_ t: T.Type) {
    let json = "{\"a\":5, \"b\" : 10}".data(using: .ascii)!
    let testCodable = try? JSONDecoder().decode(t.self, from: json)
    print("a >> \((testCodable as! Primary).a)")
    print("b >> \((testCodable as! Secondary).b)")
}

testRef(c)

这个输出是:

a >> 5
b >> 0

任何提示或指示都会感激不尽。

  • 尝试使用Xcode 9.3,Swift 4.1

1 个答案:

答案 0 :(得分:5)

Codable的魔力依赖于简单性(使用不支持继承的结构)。

自定义越多,代码越多

你必须在子类中编写一个自定义初始值设定项来考虑继承(感谢Hamish的CodingKeys和初始化程序在基类中合成的注释),我省略了Encodable部分故意

class Primary : Decodable {
    var a: Int

/*
   private enum CodingKeys: String, CodingKey { case a }

    required init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        a = try container.decode(Int.self, forKey: .a)
    }
*/
}

class Secondary : Primary {
    var b: Int

    private enum CodingKeys: String, CodingKey { case b }

    required init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        b = try container.decode(Int.self, forKey: .b)
        try super.init(from: decoder)
    }
}


func testRef<T: Decodable>() throws -> T {
    let json = "{\"a\":5, \"b\" : 10}".data(using: .utf8)!
    return try JSONDecoder().decode(T.self, from: json)
}

do {
    let secondary : Secondary = try testRef()
    print(secondary.a, secondary.b) // 5 10
} catch { print(error) }