I'm trying to get pokemon names from the pokeApi, but I have this error :
"No value associated with key CodingKeys(stringValue: "index", intValue: nil) ("index").", underlyingError: nil
import UIKit
class ListViewController: UIViewController {
@IBOutlet weak var pokemon1: UILabel!
@IBOutlet weak var pokemon2: UILabel!
@IBOutlet weak var pokemon3: UILabel!
@IBOutlet weak var pokemon4: UILabel!
@IBOutlet weak var pokemon5: UILabel!
let url = "https://pokeapi.co/api/v2/pokemon"
override func viewDidLoad() {
super.viewDidLoad()
fetchData(from: url)
}
func fetchData(from url: String) {
let url = URL(string: url)
let defaultSession = URLSession(configuration: .default)
let dataTask = defaultSession.dataTask(with: url!) { (data: Data?, response: URLResponse?, error: Error?) in
if(error != nil) {
print(String(describing: error))
return
}
var json: Pokemon
do {
json = try JSONDecoder().decode(Pokemon.self, from: data!)
Swift.print(json)
// DispatchQueue.main.async {
// self.pokemon1.text = json.name
// self.pokemon3.text = json[2].name
// self.pokemon4.text = json[3].name
// self.pokemon5.text = json[4].name
// //callback: fonction que j'appelle à la suite d'un évènement
// }
} catch {
print(error)
return
}
}
dataTask.resume() // Commence la dataTask si elle n'est pas en marche actuellement
}
struct Pokemon: Codable {
let results: Results
}
struct Results: Codable {
let name: String
let index: String
}
}
Here's the data from the api link:
{
"count": 1154,
"next": "https://pokeapi.co/api/v2/pokemon?offset=20&limit=20",
"previous": null,
"results": [
{
"name": "bulbasaur",
"url": "https://pokeapi.co/api/v2/pokemon/1/"
},
{
"name": "ivysaur",
"url": "https://pokeapi.co/api/v2/pokemon/2/"
},
{
"name": "venusaur",
"url": "https://pokeapi.co/api/v2/pokemon/3/"
},
{
"name": "charmander",
"url": "https://pokeapi.co/api/v2/pokemon/4/"
},
{
"name": "charmeleon",
"url": "https://pokeapi.co/api/v2/pokemon/5/"
},
{
"name": "charizard",
"url": "https://pokeapi.co/api/v2/pokemon/6/"
},
{
"name": "squirtle",
"url": "https://pokeapi.co/api/v2/pokemon/7/"
},
{
"name": "wartortle",
"url": "https://pokeapi.co/api/v2/pokemon/8/"
},
{
"name": "blastoise",
"url": "https://pokeapi.co/api/v2/pokemon/9/"
},
{
"name": "caterpie",
"url": "https://pokeapi.co/api/v2/pokemon/10/"
},
{
"name": "metapod",
"url": "https://pokeapi.co/api/v2/pokemon/11/"
},
{
"name": "butterfree",
"url": "https://pokeapi.co/api/v2/pokemon/12/"
},
{
"name": "weedle",
"url": "https://pokeapi.co/api/v2/pokemon/13/"
},
{
"name": "kakuna",
"url": "https://pokeapi.co/api/v2/pokemon/14/"
},
{
"name": "beedrill",
"url": "https://pokeapi.co/api/v2/pokemon/15/"
},
{
"name": "pidgey",
"url": "https://pokeapi.co/api/v2/pokemon/16/"
},
{
"name": "pidgeotto",
"url": "https://pokeapi.co/api/v2/pokemon/17/"
},
{
"name": "pidgeot",
"url": "https://pokeapi.co/api/v2/pokemon/18/"
},
{
"name": "rattata",
"url": "https://pokeapi.co/api/v2/pokemon/19/"
},
{
"name": "raticate",
"url": "https://pokeapi.co/api/v2/pokemon/20/"
}
]
}
I think I have an error from the structures... I don't really know if I need to put an array and where to put it in the code ?
CodePudding user response:
You're right, you need an array in your data structure. The results
key is for an array of Results, not a single entity.
For some reason you are also using index
rather than url
for the field name in Result
.
This should work (note: renamed Results
to Result
as it's a single item)
struct Pokemon: Codable {
let results: [Result]
}
struct Result: Codable {
let name: String
let url: String
}