Quantcast
Channel: Active questions tagged uipickerview - Stack Overflow
Viewing all articles
Browse latest Browse all 594

Populating a UIPIckerView with remote JSON array

$
0
0

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?


Viewing all articles
Browse latest Browse all 594

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>