Closed Steven4294 closed 3 years ago
How do I make
Post
conform toAnyEncodable
, so I can pass in an object as a value in my dictionary like such?
Just mark your struct as Encodable.
struct Post: Encodable {
let payload: String
}
I get
Cannot convert value of type 'String' to expected dictionary value type 'AnyEncodable'
Due to the type of checkout.email
is String, you cannot insert it directly without wrapping with the AnyEncodable initialiser.
KLPayload(
token: "",
time: 0,
customer_properties: [
"email": .init(checkout.email)
]
)
struct KLPayload: Encodable {
let token: String
let time: Int
let customer_properties: [String: AnyEncodable]
}
I am using Vapor, though I don't believe that matters. I'm trying to pass in an object something that conforms to
AnyEncodable
See code:And my post object
How do I make
Post
conform toAnyEncodable
, so I can pass in an object as a value in my dictionary like such?Additionally, I've tried:
And I get
Cannot convert value of type 'String' to expected dictionary value type 'AnyEncodable'