set a if sentence in JsonDictonary?

Hi programmers 🙂

can i set an if statement to a JSONDictionary so i can stable conditions to my JSon income-data?


what i'm trying to do in code wouldve something like this


struct Blog: Decodable {
                let Response: String
                let articles: [Article]
                let articles2: [Article2]
                let articles3: [Article3]
                let articles4: [Article4]
                
                enum CodingKeys : String, CodingKey {
                    case Response
                    
                    case articles = "Perfil_1"
                    case articles2 = "Perfil_2"
                    case articles3 = "Perfil_3"
                    case articles4 = "Perfil_4"
                }
            }
            // id, nomnbre, matricula, vehiculo, modelo, identificacion,distancia
            
            struct Article: Decodable {
                let ID: String
               // let Foto: String
                let Nombre: String
                let Matricula: String
                let Vehiculo: String
                let Modelo: String
                let Identificacion: String
                let Distancia: String
                
            }
            
            struct Article2: Decodable {
                let ID: String
              //  let Foto: String
                let Nombre: String
                let Matricula: String
                let Vehiculo: String
                let Modelo: String
                let Identificacion: String
                let Distancia: String
                
            }
            struct Article3: Decodable {
                let ID: String
             //   let Foto: String
                let Nombre: String
                let Matricula: String
                let Vehiculo: String
                let Modelo: String
                let Identificacion: String
                let Distancia: String
                
            }
            struct Article4: Decodable {
                let ID: String
              //  let Foto: String
                let Nombre: String
                let Matricula: String
                let Vehiculo: String
                let Modelo: String
                let Identificacion: String
                let Distancia: String
                
            }
            
            if response == true {
            
            do {
}
}


what my php code does, if theres a driver on 6km near the user them the code shows it as JSON, them i conect to the json and capture that data

so i manually set 4 json's to get the info(but i need 4 existant drivers *it can't be 3-2-1 or less than 4 it won't work i need 4)

so what i'm trying to do is avoid the app crash setting false the

struct Article3: Decodable {

in case of not drivers on 6km around


i want to set the json to only whose displayed



heres all my code

@IBAction func iniciar(_ sender: UIButton) {
       
        
        map.isHidden = true
        credencial1.isHidden = false
        Credencial2.isHidden = false
        Credencial3.isHidden = false
        Credencial4.isHidden = false
        
        
        let myURL = URL(string: "hehe")
        var request = URLRequest(url: myURL!)
        request.httpMethod = "POST"
        let posString = "Client_lat=\(coordLat.text!)&Client_lon=\(coordLong.text!)"
        request.httpBody = posString.data(using: .utf8)
        let task = URLSession.shared.dataTask(with: request) {
            data, response, error in
            
            if let error = error {
                print("error=\(error)")
                return
            }
            
            guard let data = data else {
                print("Something wrong")
                return
            }
           
            struct Blog: Decodable {
                let Response: String
                let articles: [Article]
                let articles2: [Article2]
                let articles3: [Article3]
                let articles4: [Article4]
                
                enum CodingKeys : String, CodingKey {
                    case Response
                    
                    case articles = "Perfil_1"
                    case articles2 = "Perfil_2"
                    case articles3 = "Perfil_3"
                    case articles4 = "Perfil_4"
                }
            }
          
            
            struct Article: Decodable {
                let ID: String
              
                let Nombre: String
                let Matricula: String
                let Vehiculo: String
                let Modelo: String
                let Identificacion: String
                let Distancia: String
                
            }
            
            struct Article2: Decodable {
                let ID: String
               
                let Nombre: String
                let Matricula: String
                let Vehiculo: String
                let Modelo: String
                let Identificacion: String
                let Distancia: String
                
            }
            struct Article3: Decodable {
                let ID: String
               
                let Nombre: String
                let Matricula: String
                let Vehiculo: String
                let Modelo: String
                let Identificacion: String
                let Distancia: String
                
            }
            struct Article4: Decodable {
                let ID: String
             
                let Nombre: String
                let Matricula: String
                let Vehiculo: String
                let Modelo: String
                let Identificacion: String
                let Distancia: String
                
            }
            
            
            
            do {
                
               
                let blog = try JSONDecoder().decode(Blog.self, from: data)
                
             
                DispatchQueue.main.async {
                    if blog.Response == "true" {
                        for article2 in blog.articles2 {
                            let id2 = article2.ID
                            UserDefaults.standard.set(id2, forKey: "IdConductor2")
                            
                            let nombre2 = article2.Nombre
                            let matricula2 = article2.Matricula
                            let vehiculo2 = article2.Vehiculo
                            let modelo2 = article2.Modelo
                            let identificacion2 = article2.Identificacion
                            let distancia2 = article2.Distancia
                            
                            self.perfil2Nombre.text = nombre2
                            self.perfil2Matricula.text = matricula2
                            self.perfil2Vehiculo.text = vehiculo2
                            self.perfil2Modelo.text = modelo2
                            self.perfil2Identificacion.text = identificacion2
                            self.perfil2Distancia.text = distancia2
                            
                           
                            for article3 in blog.articles3 {
                                let id3 = article3.ID
                                UserDefaults.standard.set(id3, forKey: "IdConductor3")
                               
                                let nombre3 = article3.Nombre
                                let matricula3 = article3.Matricula
                                let vehiculo3 = article3.Vehiculo
                                let modelo3 = article3.Modelo
                                let identificacion3 = article3.Identificacion
                                let distancia3 = article3.Distancia
                                
                                self.perfil3Nombre.text = nombre3
                                self.perfil3Matricula.text = matricula3
                                self.perfil3Vehiculo.text = vehiculo3
                                self.perfil3Modelo.text = modelo3
                                self.perfil3Identificacion.text = identificacion3
                                self.perfil3Distancia.text = distancia3
                                
                                
                                for article4 in blog.articles4 {
                                    let id4 = article4.ID
                                    UserDefaults.standard.set(id4, forKey: "IdConductor4")
                                    
                                    let nombre4 = article4.Nombre
                                    let matricula4 = article4.Matricula
                                    let vehiculo4 = article4.Vehiculo
                                    let modelo4 = article4.Modelo
                                    let identificacion4 = article4.Identificacion
                                    let distancia4 = article4.Distancia
                                    
                                    self.perfil4Nombre.text = nombre4
                                    self.perfil4Matricula.text = matricula4
                                    self.perfil4Vehiculo.text = vehiculo4
                                    self.perfil4Modelo.text = modelo4
                                    self.perfil4Identificacion.text = identificacion4
                                    self.perfil4Distancia.text = distancia4
                                    
                                   
                                    for article in blog.articles {
                                        let id = article.ID
                                        UserDefaults.standard.set(id, forKey: "IdConductor1")
                                        
                                        let nombre = article.Nombre
                                        UserDefaults.standard.set(nombre, forKey: "nombreJson")
                                        let matricula = article.Matricula
                                        UserDefaults.standard.set(matricula, forKey: "matriJson")
                                        let vehiculo = article.Vehiculo
                                        UserDefaults.standard.set(vehiculo, forKey: "vehiJson")
                                        let modelo = article.Modelo
                                        UserDefaults.standard.set(modelo, forKey: "modeloJson")
                                        let identificacion = article.Identificacion
                                        UserDefaults.standard.set(identificacion, forKey: "identiJson")
                                        let distancia = article.Distancia
                                        UserDefaults.standard.set(distancia, forKey: "distaJson")
                                        
                                        
                                        
                                        self.nombreConductor.text = nombre
                                        self.matriculaConductor.text = matricula
                                        self.vehiculoConductor.text = vehiculo
                                        self.modeloConductor.text = modelo
                                        self.numeroTaxiConductor.text = identificacion
                                        self.distanciaConductor.text = distancia
                                        break
                                    }
                                }}}} else if  blog.Response == "false" {
                        self.displayAlert(title: "Usuario", message: "no hay conductores disponibles")
                    } else if  blog.Response == "nothing" {
                        self.displayAlert(title: "Información", message: "error red")
                    }
                    
                    
                }
            } catch {
                print("Error: Couldn't decode data into Blog:", error)
                return
            }
        }
        task.resume()
        
        
        
        
        
    }



as you can see i have 4 json dictonary by default

but if theres only 3 it will crash any hints? plz :c

Accepted Reply

Not sure I understand the point.


i want to know if i can check for existant before set the text on labes.text


Do you mean, test for instance, if article2 exists in below code ? Or if id2 is not empty ? Or each field individually ?


                        for article2 in blog.articles2 {
                            let id2 = article2.ID
                            UserDefaults.standard.set(id2, forKey: "IdConductor2")
                            
                            let nombre2 = article2.Nombre
                            let matricula2 = article2.Matricula
                            let vehiculo2 = article2.Vehiculo
                            let modelo2 = article2.Modelo
                            let identificacion2 = article2.Identificacion
                            let distancia2 = article2.Distancia
                            
                            self.perfil2Nombre.text = nombre2
                            self.perfil2Matricula.text = matricula2
                            self.perfil2Vehiculo.text = vehiculo2
                            self.perfil2Modelo.text = modelo2
                            self.perfil2Identificacion.text = identificacion2
                            self.perfil2Distancia.text = distancia2


If so, why not :

if id2.isEmpty {
     continue // skip this one
} else {
    UserDefaults.standard.set(id2, forKey: "IdConductor2")
}



if !nombre2.isEmpty {
     self.perfil2Nombre.text = nombre2
}

Replies

I don't understand why you create ' struct that are absoluteley identical ?


Where do you crash ? If you have not 4 dictionaries, are all artviles (1, 2, 3, 4) defined ? Otherwise, you may crash on lines 103, 122, 141 or 160

i'm not getting any crash the app does the work.

i want to know if i can check for existant before set the text on labes.text

Not sure I understand the point.


i want to know if i can check for existant before set the text on labes.text


Do you mean, test for instance, if article2 exists in below code ? Or if id2 is not empty ? Or each field individually ?


                        for article2 in blog.articles2 {
                            let id2 = article2.ID
                            UserDefaults.standard.set(id2, forKey: "IdConductor2")
                            
                            let nombre2 = article2.Nombre
                            let matricula2 = article2.Matricula
                            let vehiculo2 = article2.Vehiculo
                            let modelo2 = article2.Modelo
                            let identificacion2 = article2.Identificacion
                            let distancia2 = article2.Distancia
                            
                            self.perfil2Nombre.text = nombre2
                            self.perfil2Matricula.text = matricula2
                            self.perfil2Vehiculo.text = vehiculo2
                            self.perfil2Modelo.text = modelo2
                            self.perfil2Identificacion.text = identificacion2
                            self.perfil2Distancia.text = distancia2


If so, why not :

if id2.isEmpty {
     continue // skip this one
} else {
    UserDefaults.standard.set(id2, forKey: "IdConductor2")
}



if !nombre2.isEmpty {
     self.perfil2Nombre.text = nombre2
}

thanks i fix it before. i forgot to publish the answer, your answer is also accurated.

thanks