I want to send empty Dictionary like this
“visitor_attrs”: {}
I try to implement empty dictionary in a class. In the decoder I get the warning:
No 'decode' candidates produce the expected contextual result type 'Dictionary'
How can I do this?
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)
}