0

I'm working on a project where I'm pulling data from an API. I understood the API's structure for an earlier call, but this one is a bit different and is giving me trouble.

Here's an example of what the API call result looks like: [1]: https://i.stack.imgur.com/V0wVh.png

I see it returning an array of steps, each of which has additional data. So here's how I built my structs:

struct Instructions: Codable {
    let steps: [Step]
}

struct Step: Codable {
    let number: Int                     // step number
    let step: String                    // step explanation
    let ingredients: [Ingredient]       // ingredients used in this step
    let image: String
}

If these are correctly structured, then I guess my question would be how can I access the data of struct Step? I was thinking of going about it like so:

I initialize this: var result: [Instructions] = [], then within the cellForRowAt function of UITableVieDataSource, I do: let stepContents = result[indexPath.row].steps. This obviously doesn't work, but I'm trying to set stepContents equal to the step: String within Step.

Any tips / thoughts would be greatly appreciated — I'm stumped. Thank you!

Edit: clarified that last paragraph.

cassilly
  • 29
  • 4

1 Answers1

0

Your Step structure isn't correct since waiting for

let image: String

but in the server response which you provided via screenshot, you don't have this field. Since you added it like non-optional, the decoder can't decode it.

try to set an optional type for an image like:

var image: String?

or remove if not needed

Simulor
  • 16
  • 3