Skip to content

Instantly share code, notes, and snippets.

@samjarman
Created August 15, 2018 10:39
Show Gist options
  • Save samjarman/8b4ef1ad60b74e1e0ec3622762531b26 to your computer and use it in GitHub Desktop.
Save samjarman/8b4ef1ad60b74e1e0ec3622762531b26 to your computer and use it in GitHub Desktop.
$curl http://pokeapi.co/api/v2/pokemon/?limit=3
// Models...
struct PokemonListResult: Codable {
let count: Int?
let previous: URL?
let results: [PokemonIndex]?
let next: URL?
}
struct PokemonIndex: Codable {
let name: String?
let detailsURL: URL?
private enum CodingKeys: String, CodingKey {
case name
case detailsURL = "url"
}
}
// In DataTask callback...
do {
let decoder = JSONDecoder()
let pokemans = try decoder.decode(PokemonListResult.self, from: data)
print(pokemans)
delegate.didSuccessfullyFetchPokemonList(withList: pokemans.results!)
} catch let err {
print("Err", err)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment