1

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!

jnpdx
  • 45,847
  • 6
  • 64
  • 94
Abhishek
  • 19
  • 1
  • 3
  • "Names of types of Pokemon" is ambiguous here. Do you want a list of all the names of all of the Pokemon in the Pokedex, or do you want a list of all of the types ("water", "fairy", etc)? Or do you want the names of the types for a specific Pokemon? This API is very famous and a great starting project. You may want to explore app.quicktype.io. Click the folder icon in the upper left (beside "JSON") and you'll find an example decoder for Pokemon. I also recommend going directly to https://pokeapi.co and figure out the query you want with their interactive "Try it now!" field. – Rob Napier Oct 23 '22 at 19:54
  • If you know you have the right query, you can also just post the output into app.quicktype.io and it'll write the decoder for you. If you still have trouble, post the JSON you're trying to decode and the struct you want to get out, and we can help you write a custom Decoder – Rob Napier Oct 23 '22 at 19:59
  • Thank you for your response, I indeed want the names of types for a specific pokemon, such that when the user clicks on a pokemon in list view it should display the name of types of that specific pokemon on detail view. for example: when i click on bulbasur i should display its types, which are "grass" and "poison", if I click on pidgeot, then display its types "normal", "flying" like that. I have the appropriate structs but i have problems in processing the data to retrive the correct values and display it on screen, could you help me with this? – Abhishek Oct 23 '22 at 22:30
  • Have a look at these SO posts/answers, regarding retrieving Pokemon info from an API. https://stackoverflow.com/questions/74084510/swiftui-onappear-not-firing-as-expected-when-i-load-a-new-page-of-visible-json/74091573#74091573 and https://stackoverflow.com/questions/73187906/stuck-with-figuring-out-how-to-use-api-response-to-make-a-call-to-retrieve-a-res/73188138#73188138 – workingdog support Ukraine Oct 23 '22 at 23:48

0 Answers0