My app is decoding some JSON data from a PHP web service but the data is not showing on the labels when I click the fetch button for the first time.
In debug I can see that the data is passed to the array but it's not passed to the labels in the view controller.
Can you please help me on this?
Thanks in advance,
Adelmo
Button click
@IBAction func btn_ler(_ sender: Any)
{
//txt_matricula.text = ""
//txt_marca.text = ""
//txt_modelo.text = ""
obter_dados_veiculo(matricula: input_matricula.text!)
for veiculo in a_veiculos
{
txt_matricula.text = veiculo.MATRICULA
txt_modelo.text = veiculo.MODELO
txt_marca.text = veiculo.MARCA
}
//lbl_num_servicos.text = String( a_veiculos.count)
}
Function to retrieve the JSON
func obter_dados_veiculo (matricula : String)
{
//Variável que vai conter o URL
let v_url = "http://arshome.hopto.org/Domingues/DEV/api_get_veiculos.php?MATRICULA=" + matricula
let o_url = URL(string: v_url)
URLSession.shared.dataTask(with: o_url!)
{
(data, response, error) in
do
{
let matriculas = try JSONDecoder().decode([s_veiculo].self, from: data!)
//for matricula in matriculas
//{
//print(matricula.MATRICULA)
a_veiculos = []
a_veiculos.append(contentsOf: matriculas)
//}
}
catch
{
print("We have an error!")
//self.txt_matricula.text = "We have an error!"
}
print( a_veiculos.count)
}.resume()
}