我想像这样发送空字典
“visitor_attrs”: {}
我尝试在一个类中实现空字典。在解码器中,我收到警告:
没有“解码”候选产生预期的上下文结果类型“字典”
我怎样才能做到这一点?
var data: String
var event: String
var visitorAttrs: Dictionary<String, Any>
init(data: String, event: String) {
self.data = data
self.event = event
self.visitorAttrs = [:]
}
private enum CodingKeys: String, CodingKey {
case data
case event
case visitorAttrs = "visitor_attrs"
}
required public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.data = try container.decode(String.self, forKey: .data)
self.event = try container.decode(String.self, forKey: .event)
self.visitorAttrs = try container.decode(Dictionary<String:Any>.self, forKey: .visitorAttrs)
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(self.data, forKey: .data)
try container.encode(self.event, forKey: .event)
try container.encode(self.visitorAttrs, forKey: .visitorAttrs)
}