I am an absolute beginner learning to program in swift. I am currently making a pokedex app for pokemons using the poke api: https://pokeapi.co/ When i navigate from pokemon list view to detail page of pokemon, I am unable to retrive the "types" of pokemon. I want to retrive the name of the types of pokemon. I have tried everything so far but I am not getting anywhere.
The code so far:
struct DetailPokemon: Codable {
let id: Int
let height: Int
let weight: Int
let types: [PokemonType]
}
struct PokemonType: Codable {
let slot: Int
let type: Species
}
struct Species: Codable {
let name: String
let url: String
}
Retrive pokemon by is from the api
func getDetailPokemon(id: Int, _ completion: @escaping (DetailPokemon) -> ()) {
Bundle.main.fetchData(
url: "https://pokeapi.co/api/v2/pokemon/\(id)/",
model: DetailPokemon.self) { data in
completion(data)
} failure: { error in
print(error)
}
}
using viewmodel to process data and call method in view
func getPokemonIndex(pokemon: Pokemon) -> Int {
if let index = self.pokemonList.firstIndex(of: pokemon) {
return index + 1
}
return 0
}
func getDetails(pokemon: Pokemon) {
let id = getPokemonIndex(pokemon: pokemon)
self.pokemonDetails = DetailPokemon(id: 0, height: 0, weight: 0, types: [])
pokemonManager.getDetailPokemon(id: id) { data in
DispatchQueue.main.async {
print(data)
//self.pokemonDetails = data
return DetailPokemon(data.id, data.weight, data.height, data.types { type in
})
}
}
}
Any help will be highly appreciated. Many Thanks!