Given the following JSON document I'd like to create a struct
with four properties: filmCount
(Int), year
(Int), category
(String), and actor
(Actor array).
{
"filmCount": 5,
"year": 2018,
"category": "Other",
"actors":{
"nodes":[
{
"actor":{
"id":0,
"name":"Daniel Craig"
}
},
{
"actor":{
"id":1,
"name":"Naomie Harris"
}
},
{
"actor":{
"id":2,
"name":"Rowan Atkinson"
}
}
]
}
}
PlacerholderData
is a struct storing the three main properties and the list of actors which should be retrieved from the nested nodes
container within the actors
property from the JSON object.
PlacerholderData:
struct PlaceholderData: Codable {
let filmCount: Int
let year: Int
let category: String
let actors: [Actor]
}
Actor.swift:
struct Actor: Codable {
let id: Int
let name: String
}
I am attempting to do this through providing my own init
to initialise the values from the decoder's container manually. How can I go about fixing this without having to have an intermediate struct storing a nodes
object?