我想从一个具有默认实现属性的可编码结构中创建一个字典。
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
的How can I use Swift’s Codable to encode into a dictionary?扩展:
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):
["defaultValue": "my-default-value", "value": "my-value"]
合成编码器仅考虑结构中的成员,而不考虑协议扩展中的任何属性或计算属性。
您必须编写自定义初始化程序。我宁愿让结构采用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 { ...
Encodable
不会识别计算属性。要解决此问题,请覆盖官方文档encode(to:)
中显示的https://developer.apple.com/documentation/foundation/archives_and_serialization/encoding_and_decoding_custom_types函数
编辑:问题的可能解决方案:How to use computed property in a codable struct (swift)
这是因为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)
}
}