This is similar to for example this question: How can I use Swift’s Codable to encode into a dictionary?, except that I want to specifically turn it into a [String: CustomStringConvertible]
dictionary and I am not able to.
struct Model: Codable {
let a: String
let b: Int
}
let jsonData = try JSONEncoder().encode(Model(a: "a", b: 1))
let parameters = try JSONSerialization.jsonObject(with: jsonData, options: .allowFragments)
parameters as? [String: CustomStringConvertible] // nil
How can I make this work? This for example does work, which should work the same you'd think:
let dict: Any = ["a": "a", "b": 1]
dict as? [String: CustomStringConvertible] // ["a": "a", "b": 1]