首页 > 解决方案 > 如何将协议属性默认实现编码为字典

问题描述

我想从具有默认实现属性的可编码结构中制作字典。

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

为此,我使用如何使用 Swift 的 Codable 编码到字典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 { $0 as? [String: Any] }
    }
}

因此,当我实例化结构并将其“编码”为字典时:

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

那么defaultValue不包括:

["value": "my-value"]

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

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

标签: swiftswift-protocolsencodable

解决方案


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

您必须编写一个自定义初始化程序。而且我更愿意让结构采用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 { ...

推荐阅读