如何将协议 属性 默认实现编码为字典

How to encode protocol property default implementation to dictionary

我想用默认实现 属性 从可编码结构创建字典。

struct MyStruct: MyStructProtocol {
    var value: String
}

该结构实现了一个协议。该协议有两个变量。一个变量有一个默认实现。

protocol MyStructProtocol: Encodable {
    var defaultValue: String { get }
    var value: String { set get }
}

extension MyStructProtocol {
    var defaultValue: String { return "my-default-value" }
}

为此,我使用 Encodable 来自 的扩展:

extension Encodable {
    var asDictionary: [String: Any]? {
        guard let data = try? JSONEncoder().encode(self) else { return nil }
        return (try? JSONSerialization.jsonObject(with: data, options: .allowFragments)).flatMap { [=13=] as? [String: Any] }
    }
}

因此,当我实例化结构并将其 "encode" 放入字典时:

let myStruct = MyStruct(value: "my-value")
let myStructDictionary = myStruct.asDictionary

则不包含defaultValue:

["value": "my-value"]

但我需要的是(包括默认值):

["defaultValue": "my-default-value", "value": "my-value"]

Encodable 将无法识别计算属性。要解决此问题,请重写 encode(to:) 函数,如官方文档 https://developer.apple.com/documentation/foundation/archives_and_serialization/encoding_and_decoding_custom_types

中所示

编辑:问题的可能解决方案:

合成编码器仅考虑结构中的成员,不考虑协议扩展中的任何属性或计算属性。

您必须编写自定义初始化程序。而且我更愿意让结构采用 Encodable 而不是协议。

struct MyStruct: MyStructProtocol, Encodable {
    var value: String

    private enum CodingKeys: String, CodingKey { case value, defaultValue }

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

protocol MyStructProtocol { ...

这是因为 defaultValue 的默认值已在协议的扩展中实现,这意味着它是 计算的 属性.

struct MyStruct: MyStructProtocol {
    var value: String

    enum CodingKeys: String, CodingKey {
        case value
        case defaultValue = "my-default-value"
    }

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