Skip to content

Instantly share code, notes, and snippets.

@jarrodnorwell
Last active September 29, 2022 21:19
Show Gist options
  • Select an option

  • Save jarrodnorwell/2760c57d760b85a90209c1b7a125d3d7 to your computer and use it in GitHub Desktop.

Select an option

Save jarrodnorwell/2760c57d760b85a90209c1b7a125d3d7 to your computer and use it in GitHub Desktop.
import Foundation
struct Movie : Codable, Hashable {
// ...
}
struct MovieResults : Codable, Hashable {
let results: [Movie]
}
enum TMDBAPIError : Error {
case invalidURL
}
class TDMBAPI {
static let shared = TMDBAPI()
func getPopularMovies() async throws -> [Movie] {
guard let url = URL(string: "...") else {
throw TMDBAPIError.invalidURL
}
let (data, _) = try await URLSession.shared.data(for: url)
return JSONDecoder().decode(MovieResults.self, from: data).results
}
}
@jarrodnorwell

jarrodnorwell commented Sep 29, 2022

Copy link
Copy Markdown
Author

Fairly sure MovieResults does not need to have Hashable however, I’ve added it anyway.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment