I am looking to make multiple API calls using swift to get my data from the TMDB API. I have created the struct and the code I am using is this:
import Foundation
public class LoadTMDBMovies{
// Create an array of TMDBMovies data to store the results
var moviesList = [MoviesIndividual]()
let tmdbId = LoadLinksData().linksJSON
func fetch(){
for tmdbIds in tmdbId{
let id = tmdbIds.tmdbId
let api = "https://api.themoviedb.org/3/movie/" + String(id) + "?api_key="
let url = URL(string: api)!
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
// get the data
guard let data = data, error == nil else {return}
do{
let moviesData = try JSONDecoder().decode(MoviesIndividual.self, from: data)
self.moviesList.append(moviesData)
}
catch{
let error = error
print(error.localizedDescription)
}
};task.resume()
}
}
}
I am trying to loop through all the items in my array get the TMDB Id and replace it in the URL where necessary. This doesn't give me an error when I run it just a warning that my tmdbId was never used and prints a count of 0 when I tested it out.