0

When I receive data from a json I stored inside an array so that I can iterate that array and store each value inside a dictionary type and assign each value using the dict key, the problem is that one of the values is an INT type, if I try to downcast the value I receive this error Could not cast value of type '__NSCFNumber' (0x1112b2948) to 'NSString'and If I try to do a optional downcast the value is passed as a nil. I specifically need this Int value as a String value, but I don't know how to perform this cast of types, any suggestion? Heres the code:

func getRoutesWS(_ sucursal: Int, date consultDate: Date){
        do{
            routesArray = .init()
            
            let urlString: String = String(format: "webserviceURL", sWebService!, sSucursal!, dateFormat.string(from: consultDate), groupID!)
            NSLog("%@", urlString)
            
            let jsonURL: URL? = .init(string: urlString)
            var data: Data? = nil
            var array: Array<Any> = []
            var json: Dictionary =  Dictionary<AnyHashable, Any>.init()
            
            if let jsonURL = jsonURL {
                data = try Data.init(contentsOf: jsonURL, options: .uncached)
            }
            if let data = data {
                json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as! Dictionary
            }
            array = json["obtenerDetalleRutasResult"] as! Array<Any>
            
            if array.count != 0 {
                var validateData: [AnyHashable: Any] = .init()
                
                for value in 0..<(array.count){
                    validateData = array[value] as! [AnyHashable: Any]
                    
                    let routeDetail = DetalleRuta(
                        routeID: validateData["id_ruta"] as? String,
                        name: validateData["nombre"] as? String,
                        nameInCharge: validateData["nombre_enc"] as? String,
                        semaphore: validateData["semaforo"] as? String,
                        exit: validateData["salida"] as? String,
                        entry: validateData["entrada"] as? String,
                        serviceOrderID: validateData["id_orden_servicio"] as? String,
                        visitedInstances: validateData["is_visitadas"] as? String,
                        authorizedInstances: validateData["is_autorizadas"] as? String,
                        lastSiteExit: validateData["salida_ultimo"] as? String,
                        enterBD: validateData["subir_bd"] as? String,
                        captureType: validateData["tipo_captura"] as? String,
                        truckVariation: validateData["variacion_camion"] as? String
                    )
                    
                    if value != 0 {
                      
                        let counter = routesArray.count
                        if validateData["id_ruta"] as? String != (routesArray[counter - 1].rutaID) {
                            routesArray.append(routeDetail)
                        }
                     
                    } else {
                        routesArray.append(routeDetail)
                    }
                }
            }
            
        } catch {
            let alert: UIAlertController = .init(title: "Alerta", message: "Existen problemas con la conexion", preferredStyle: .alert)
            alert.addAction(UIAlertAction.init(title: "Aceptar", style: .default, handler: nil))
            self.present(alert, animated: true, completion: nil)
        }
    }

The issue is at line routeID: validateData["id_ruta"] as? String

routeIDis an String I need to use later in the code, and the value coming from validateData["id_ruta"]its the value that is a Int

0 Answers0