Closed Arcovv closed 7 years ago
Although dictionaries can't be Decodable
, there are many different ways of decoding them. For instance, you should be able to do this:
let weightDirectory: [String: Int] = try json => "weights"
return weightDirectory.flatMap(ReaderDirectoryWeight.init)
Thank you!
This is my struct:
struct ReaderDirectoryWeight { let sourceName: String let weight: Int }
The "sourceName" is assigned by a dictionary key, and the "weight" is assigned by the key value.
There is the example:
"weights": { "Text/Cover.xhtml": 4, "Text/Introduction.xhtml": 209, "Text/AboutAuthor.xhtml": 423, "Text/Part1.xhtml": 428, "Text/Ch1-01.xhtml": 2234, "Text/Ch1-02.xhtml": 3947 }
It's my solution:
` typealias ReaderDirectoryWeightType = [String: Int]
static func decode(_ json: Any) throws -> [ReaderDirectoryWeight] {
}`
Has a better solution? Thanks!