I'm playing around with the new Codable
protocol and JSONEncoder
The following method throws an odd exception:
private func generateJSONData(body: [String: Codable]?) -> Data? {
guard let body = body else {
print("empty body for json encoding")
return nil
}
let encoder = JSONEncoder()
do {
let data = try encoder.encode(body)
return data
} catch {
print("error encoding body to json")
return nil
}
}
fatal error: Dictionary<String, Decodable & Encodable> does not conform to Encodable because Decodable & Encodable does not conform to Encodable.
This even gets funnier when I change the signature to
private func generateJSONData(body: [String: Encodable]?)
fatal error: Dictionary<String, Encodable> does not conform to Encodable because Encodable does not conform to Encodable.:
The interesting thing is that this works fine with [String: String]
Is there a reason [String: Codable]
is not codable?