The following code used to work fine in Swift 4.0:
let dict: [String: Encodable] = ["one": 1]
let encodable: Encodable = dict
With 4.1 this now results in the error:
Value of type '[String : Encodable]' does not conform to specified type 'Encodable'
In my search of a workaround, I tried
let encodable: Encodable = dict as! Encodable
but this results in a segmentation fault.
Can anyone recommend a workaround?
dlbuckley
(Dale Buckley)
April 4, 2018, 8:38am
2
Encodable
doesn't conform to Encodable
and never did. The errors are just now more robust with the introduction of the conditional conformance added in 4.1.
Unfortunately you either need to specify the type or use a method of fuzzy matching like with AnyCodable or JSON .
You can read more about this on these threads:
Hi Jon,
I just joined this mailing list and have tried to catch up on the
history of this thread, so please excuse me if I’ve missed something.
I’m sorry the Codable API at the moment does not answer your needs —
you’re clearly not the only one who’s run into this, so let’s see
how we can work together to make the API better for everyone.
For one thing, in the case of grabbing a subtree of JSON as
"unevaluated" or "unmapped" (as it appears to be in the metadata case),
it should be fairl…
And:
Currently it is impossible to ask a Decoder for some unevaluated data from it's storage. This problem arrises, if someone needs this data to re-encode it later, but does not know how it is structured. The problem was described in SR-53112 .
A Unevaluated type has also been discussed in relation to this question .
I am opening this topic, because the discussion seems no longer directly relevant to this other problem to me.
Dynamic Member Lookup
I recommend you using such an enum. Depending on what you want to do with it, I think it isn‘t such a bad structure at all (depending on what you are going to do with it).
dlbuckley
(Dale Buckley)
April 4, 2018, 9:32am
4
I agree, we actually moved from AnyCodable to something similar to JSON as it was just more predictable and manageable.