【发布时间】:2020-04-29 12:15:17
【问题描述】:
我正在从远程服务器下载 JSON 数组:
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()
}
我有一个 Colores 的结构类:
struct Colores:Decodable {
let id: Int
let nombre: String
let icono: String
let modelo: Int
let caracterista: Int
}
我需要用解码的 JSON 对象填充 PickerView,将字段名称显示为标题并存储来自 pickerview 所选项目的字段 id。
我在viewDidLoad中调用downloadJSONColores方法如下:
downloadJSONColores {
coloresPV.reloadData()
}
其中coloresPV 是我的pickerView,但显然这种方式只适用于collectionViews,不适用于pickerViews。 在执行 downloadJSONColores 时填充 pickerView 的最佳方式是什么?
【问题讨论】:
标签: ios swift uipickerview