I have a struct like:
struct Demo: Codable {
var foo: String
var bar: String
...
}
And I have an array of demo:
let array = [Demo(foo: "a", bar: "b"), Demo(foo: "c", bar: "d")]
And I want to convert this array to a Dictionary [[String: Any]] with something like this:
let dictionary:[[String : Any]] = array.toDictionaryFromArrayOfCodable()
How can I get it?
Edit: My expected output is something like, and I want to use JSONEncoder:
[["foo": "a", "bar": "b"], ["foo": "c", "bar": "d"]]