Closed RichMarketApp closed 3 years ago
Just wrap the values with .init
like this
var body: [String: AnyEncodable]? =
["string": .init(data.string),
"int": .init(data.int),
"double": .init(data.double),
"bool": .init(data.bool)]
Custom types like AnyEncodable
requires explicit type casting, because the Swift compiler does not know that how to cast random type to the custom type, unless the (random) type inherits custom type.
It is easy to understand what is going on when we look more closer. 🔎
let string: String = "Hello"
let int: Int = 42
let anyEncodableString = string as AnyEncodable // Error: Cannot convert value of type 'String' to type 'AnyEncodable' in coercion
let anyEncodableInt = int as AnyEncodable // Error: Cannot convert value of type 'Int' to type 'AnyEncodable' in coercion
let anyEncodableFloat = 3.14159 as AnyEncodable // No problem because `3.14159` is a Float literal
let anyEncodableBool = true as AnyEncodable // No problem because `true` is a Boolean literal
We know that string
and int
are from their literal types, but compiler does not think so. string
is String
typed value, not a String literal. int
is Int
typed value, not an Integer literal.
…
Q. Wait, but AnyHashable
does not not make any compile error. How to explain about this?
Because AnyHashable
is a member of Swift Standard Library. It is explicitly defined in Swift itself to implicitly wrap the value with AnyHashable
. But AnyEncodable
is not. We cannot make any implicit things without modifying Swift itself.
Hi, I have a problem with assigning data to the dictionary that should be encodable. The Data struct inherit from Codable protocol and contains four different variable types.
I am trying to convert this struct to dictionary that will represent some other model. Please find a dummy code below:
When the project is compiling I am getting four errors:
Is there any simple way to fix it and left the code clean?