I am using JSONDecoder to access JSON data through an API. Within this JSON Data are several [arrays]. I am running into a problem of accessing each instance that a key appears.
Here is the code:
var details = [Details]()
var production = [Production]()
struct Details: Codable {
let title: String
let poster_path: String?
let id: Int?
let production_companies: [Production]
}
struct Production: Codable {
let name: String
}
let task = session.dataTask(with: request, completionHandler: { (dataOrNil, response, error) in
if let data = dataOrNil {
do { let details = try! JSONDecoder().decode(Details.self, from: data)
let production = details.production_companies
print(production)
}
}
})
Here is what print(production) prints to the console:
[Film_Bee.DetailsView.Production(name: "Columbia Pictures"), Film_Bee.DetailsView.Production(name: "Marvel Entertainment"), Film_Bee.DetailsView.Production(name: "Sony Pictures")]
What I'm trying to do is access each name within the array. I know to access the first one I can use production.first?.name but if I am unsure how to access each one to place into a single label.