This may be a noob question, but I am trying to make an API call with the trakt.tv API. However, no data is showing up, and I am unsure why this is occurring. This is my view code:
import SwiftUI
struct ContentView: View {
@State private var trendingMovies = [TrendingMovie]()
var body: some View {
NavigationView {
List(trendingMovies, id: \.movie.ids.tmdb ) { trendingMovie in
VStack {
Text(trendingMovie.movie.title)
Text("\(trendingMovie.movie.year)")
}
}
.navigationTitle("Hello")
.task {
await fetchTrendingMovies()
}
}
}
func fetchTrendingMovies() async {
guard let url = URL(string: "https://api.trakt.tv/movies/trending") else {
print("Could not get trending movies.")
return
}
do {
let (data, _) = try await URLSession.shared.data(from: url)
if let decodedResponse = try? JSONDecoder().decode([TrendingMovie].self, from: data) {
print("We got the trending movies.")
trendingMovies = decodedResponse
}
} catch {
print("Could not get trending movies.")
}
}
}
This is my model code:
struct TrendingMovie: Codable {
let watchers: Int
let movie: Movie
}
struct Movie: Codable {
let title: String
let year: Int
let ids: IDS
}
struct IDS: Codable {
let trakt: Int
let slug: String
let imdb: String
let tmdb: Int
}
I am still new to this stuff, so any help would be greatly appreciated. This is the first time I am working with an API on my own.