Hi,
I have a dictionary defined as [String:Encodable].
I'm trying to encode it using JSONEncoder().encode().
When I do this I get a compiler error, "Type 'any Encodable' cannot conform to 'Encodable'".
What does this mean and how do I fix it?
Hi,
I have a dictionary defined as [String:Encodable].
I'm trying to encode it using JSONEncoder().encode().
When I do this I get a compiler error, "Type 'any Encodable' cannot conform to 'Encodable'".
What does this mean and how do I fix it?
Could you show code, that would help.
Consider this:
typealias MyDict = [String:Encodable]
let myDict = MyDict()
if let encoded = try? JSONEncoder().encode(myDict) {
print("encoded")
}
Causes the error you get.
But this works with a concrete type:
struct Foo: Encodable {
public let a: String
}
let foo = Foo(a: "foo")
let encoder = JSONEncoder()
let data = try encoder.encode(foo)
typealias MyDict = [String:Foo]
let myDict = MyDict()
if let encoded = try? JSONEncoder().encode(myDict) {
print("encoded", encoded)
}
Otherwise, look at this:
or