[Solved] Using JSON Decodable in Swift 4.1


Your previous question is quite close but you have to add the struct for the root object

Declare the struct members non-optional as much as possible. The URLs can be decoded as URL

struct Root : Decodable {
    let count : Int 
    let recipes : [Recipe]
}

struct Recipe : Decodable { // It's highly recommended to declare Recipe in singular form
    let recipeId : String
    let imageUrl, sourceUrl, f2fUrl : URL
    let title : String
    let publisher : String
    let socialRank : Double
    let page : Int?
    let ingredients : [String]?
}

Now decode

let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let result = try decoder.decode(Root.self, from: data)
self.recipes = result.recipes

3

solved Using JSON Decodable in Swift 4.1