I am downloading a JSON Array from a remote server:
var colores = [Colores]()func downloadJSONColores(completed: @escaping () -> ()) { let url = URL(string: "https://../colores.php") URLSession.shared.dataTask(with: url!) { (data,response,error) in print(data as Any) print(response as Any) if error == nil { do { self.colores = try JSONDecoder().decode([Colores].self, from: data!) DispatchQueue.main.async { completed() } }catch{ } } }.resume() }
I have a struct class for Colores:
struct Colores:Decodable { let id: Int let nombre: String let icono: String let modelo: Int let caracterista: Int}
What I need is to populate a PickerView with the decoded JSON objects, showing the field nombre as title and storing the field id from the pickerview selected item.
I am calling the downloadJSONColores method in viewDidLoad as follows:
downloadJSONColores { coloresPV.reloadData() }
where coloresPV is my pickerView, but obviously this way of doing this only works for collectionViews, not for pickerViews. Which would be the best way to populate the pickerView while executing downloadJSONColores?