![]() If you know the incoming dictionary keys, and you know the JSON types of all the values at all levels, the best solution is to declare a hierarchy of custom types that individually conform to Codable, relying on synthesis of the conforming implementations so you don't have to write them yourself. What you do about this depends on the nature of your JSON data. Swift defines a number of these type-erased type (such as AnyHashable, conforming to the protocol Hashable), but there isn't one for Decodable. For now, what you would need instead is a "type-erased type" that conforms to Decodable. It doesn't work in decode(.self, from: data) because in Swift, protocols don't conform to themselves, for technical reasons. Your Foo typealias isn't really necessary, because "Any & Decodable" is basically the same thing as just "Decodable". In the current version of Swift, it's possible to have conditional conformance, since the idea of "this collection is Codable if the types of its elements are Codable" can now be expressed at compile time. ![]() At that time, arrays and dictionaries declared conformance with Codable, but failed at run time if their contents weren't Codable. The situation has changed a bit since the thread you linked to. So, for informational purposes, let's tease this apart: ![]() It's not that no one sees it as a problem (one way another, this issue keeps coming up on ), but that the Swift team is still some way away from providing a solution.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |