0

I am trying to decode some JSON in swift, but I keep getting an error, which I am assuming is because my struct is not written correctly. Any ideas? I am calling this API https://pokeapi.co/api/v2/pokemon/1/

Here is the json decode call:

func fetchPokemonImages() {
        
        for var pokemons in self.Pokemons {
            
            let defaultSession = URLSession(configuration: .default)
            if let url = URL(string: pokemons.url) {
              let request = URLRequest(url: url)
              let dataTask = defaultSession.dataTask(with: request) { (data, response, error) in
                guard let pokemonImage = try? JSONDecoder().decode(PokemonImageList.self, from: data!) else {
                  print(error)
                  return
                }
                DispatchQueue.main.async {
                    
                    self.PokemonsImage.append(contentsOf: pokemonImage.sprites)
                    self.PokemonTableView.reloadData()             }
              }
                dataTask.resume()
            }
        }
    }

Here are my structs:

struct PokemonImage: Codable {
    let back_default: String

    enum CodingKeys: String, CodingKey {
        case backDefault = "back_default"
    }

    init(backDefault: String) {
        self.backDefault = backDefault
    }

    init(from decoder: Decoder) throws {
        let postsContainer = try decoder.container(keyedBy: CodingKeys.self)
        backDefault = try postsContainer.decode(String.self, forKey: .backDefault)
    }
}

struct PokemonImageList: Codable {
    
    let sprites: [PokemonImage]

    enum CodingKeys: String, CodingKey {
        case sprites
    }
}
2
  • 1
    What’s the error message? Commented Feb 28, 2021 at 17:17
  • 3
    Why are you still doing try? instead of try? Are you aware that the error you are printing when the decoding fails is the parameter to the closure so it has nothing to do with the decoding. Commented Feb 28, 2021 at 17:25

1 Answer 1

1

from the API your share sprites is an object, not an array

"sprites": {
        "back_default": "https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/back/1.png",
        "back_female": null,
        "back_shiny": "https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/back/shiny/1.png",
        "back_shiny_female": null,
        "front_default": "https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/1.png",
        "front_female": null,
        "front_shiny": "https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/shiny/1.png",
        "front_shiny_female": null,
        "other": {
            "dream_world": {
                "front_default": "https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/other/dream-world/1.svg",
                "front_female": null
            },
            "official-artwork": {
                "front_default": "https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/other/official-artwork/1.png"
            }
        }

so sprites in PokemonImageList should be an object, not an array

struct PokemonImageList: Codable {
    
    let sprites: PokemonImage

    enum CodingKeys: String, CodingKey {
        case sprites
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

I think this might be the issue, thanks. I'm having another error now though related to appending the codeable object pokemonImage to the array PokemonsImage. (Instance method 'append(contentsOf:)' requires that 'PokemonImage' conform to 'Sequence')
use append(_ newElement: Element) to append 1 object, append(contentsOf:) is used to append an array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.