[ad_1]
I need to return Pokemon names from PokeApi, however I’ve this error :
Worth of sort ‘ListViewController.Pokemon’ has no subscripts.
Here is my code :
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() {
tremendous.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!) { (information: Information?, response: URLResponse?, error: Error?) in
if(error != nil) {
print(String(describing: error))
return
}
var json: Pokemon
do {
json = strive JSONDecoder().decode(Pokemon.self, from: information!)
Swift.print(json)
DispatchQueue.important.async {
// self.pokemon1.textual content = json[0].identify
self.pokemon2.textual content = json[1].outcomes.identify
// self.pokemon3.textual content = json[0].outcomes.identify
// self.pokemon4.textual content = json[3].identify
// self.pokemon5.textual content = json[4].identify
//callback: fonction que j'appelle à la suite d'un évènement
}
} catch {
print(error)
return
}
}
dataTask.resume() // Start la dataTask si elle n'est pas en marche actuellement
}
struct Pokemon: Codable {
let outcomes: [Result]
}
struct Outcome: Codable {
let identify: String
let url: String
}
}
I checked with the print and I’m receiving the json information. this is what’s within the print :
Pokemon(outcomes: [RattrapageIOS.ListViewController.Result(name: "bulbasaur", url: "https://pokeapi.co/api/v2/pokemon/1/"), RattrapageIOS.ListViewController.Result(name: "ivysaur", url: "https://pokeapi.co/api/v2/pokemon/2/"), RattrapageIOS.ListViewController.Result(name: "venusaur", url: "https://pokeapi.co/api/v2/pokemon/3/"), RattrapageIOS.ListViewController.Result(name: "charmander", url: "https://pokeapi.co/api/v2/pokemon/4/"), RattrapageIOS.ListViewController.Result(name: "charmeleon", url: "https://pokeapi.co/api/v2/pokemon/5/"), RattrapageIOS.ListViewController.Result(name: "charizard", url: "https://pokeapi.co/api/v2/pokemon/6/"), RattrapageIOS.ListViewController.Result(name: "squirtle", url: "https://pokeapi.co/api/v2/pokemon/7/"), RattrapageIOS.ListViewController.Result(name: "wartortle", url: "https://pokeapi.co/api/v2/pokemon/8/"), RattrapageIOS.ListViewController.Result(name: "blastoise", url: "https://pokeapi.co/api/v2/pokemon/9/"), RattrapageIOS.ListViewController.Result(name: "caterpie", url: "https://pokeapi.co/api/v2/pokemon/10/"), RattrapageIOS.ListViewController.Result(name: "metapod", url: "https://pokeapi.co/api/v2/pokemon/11/"), RattrapageIOS.ListViewController.Result(name: "butterfree", url: "https://pokeapi.co/api/v2/pokemon/12/"), RattrapageIOS.ListViewController.Result(name: "weedle", url: "https://pokeapi.co/api/v2/pokemon/13/"), RattrapageIOS.ListViewController.Result(name: "kakuna", url: "https://pokeapi.co/api/v2/pokemon/14/"), RattrapageIOS.ListViewController.Result(name: "beedrill", url: "https://pokeapi.co/api/v2/pokemon/15/"), RattrapageIOS.ListViewController.Result(name: "pidgey", url: "https://pokeapi.co/api/v2/pokemon/16/"), RattrapageIOS.ListViewController.Result(name: "pidgeotto", url: "https://pokeapi.co/api/v2/pokemon/17/"), RattrapageIOS.ListViewController.Result(name: "pidgeot", url: "https://pokeapi.co/api/v2/pokemon/18/"), RattrapageIOS.ListViewController.Result(name: "rattata", url: "https://pokeapi.co/api/v2/pokemon/19/"), RattrapageIOS.ListViewController.Result(name: "raticate", url: "https://pokeapi.co/api/v2/pokemon/20/")])
What appears to be the problem ?
[ad_2]
