Skip to main content
se añadieron 3589 caracteres en el cuerpo
Origen Enlace
Angel
  • 262
  • 1
  • 9

[Actualizacion] prueba con esto

override func viewDidLoad() {
        super.viewDidLoad()
        self.get_JSon()
    }
    
    func get_JSon()
    {
        self.url_request()
        { json in
            if let arreglo_json = json
            {
                if let message = arreglo_json["Message"] as? String,
                   let isError = arreglo_json["IsError"] as? Bool,
                   let data    = arreglo_json["Data"] as? NSArray
                {
                    print("Message: \(message) \nisError: \(isError)")
                    for item in data
                    {
                        if let data_item = item as? [String: AnyObject]
                        {
                            if let cGuid = data_item["cGuid"] as? String,
                               let cName = data_item["cName"] as? String,
                               let bActive = data_item["bActive"] as? Int
                            {
                                let created_at = data_item["created_at"]
                                let updated_at = data_item["updated_at"]
                                let default_value = "null" as AnyObject
                                
                                print("cGuid: \(cGuid) \ncName: \(cName) \nbActive: \(bActive) \ncreated_at: \(created_at ?? default_value) \nupdated_at: \(updated_at ?? default_value) \n")
                            }
                        }
                    }
                }
            }
        }
    }
    
    func url_request(completion: @escaping(NSDictionary?) -> ())
    {
        let url_request = URLRequest(url: URL(string: "http://192.168.203.141/")!)
        var arreglo: NSDictionary?
        
        URLSession.shared.dataTask(with: url_request)
        { (data, response, error) in
            if error != nil
            {
                print(error ?? "Error")
            }
            else
            {
                if let url_response = response as? HTTPURLResponse
                {
                    if url_response.statusCode == 200
                    {
                        do
                        {
                            arreglo = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
                        }
                        catch let error as NSError
                        {
                            print(error.localizedDescription)
                        }
                    }
                    else
                    {
                        print(url_response.statusCode)
                    }
                }
            }
            completion(arreglo)
        }.resume()
    }

Realice todo el proceso usando una maquina virtual y funciona :D mi error al obtener datos de NSArray sin antes transformar cada item a [String: AnyObject]

Esto es lo que imprime:

Message: Ok 
isError: false
cGuid: 641f42d7-dfa8-11e7-95da-ec086b0540b2 
cName: Restaurant 
bActive: 1 
created_at: <null> 
updated_at: <null> 

cGuid: 6e71b498-dfa8-11e7-95da-ec086b0540b2 
cName: Restaurant 
bActive: 1 
created_at: <null> 
updated_at: <null> 

[Actualizacion] prueba con esto

override func viewDidLoad() {
        super.viewDidLoad()
        self.get_JSon()
    }
    
    func get_JSon()
    {
        self.url_request()
        { json in
            if let arreglo_json = json
            {
                if let message = arreglo_json["Message"] as? String,
                   let isError = arreglo_json["IsError"] as? Bool,
                   let data    = arreglo_json["Data"] as? NSArray
                {
                    print("Message: \(message) \nisError: \(isError)")
                    for item in data
                    {
                        if let data_item = item as? [String: AnyObject]
                        {
                            if let cGuid = data_item["cGuid"] as? String,
                               let cName = data_item["cName"] as? String,
                               let bActive = data_item["bActive"] as? Int
                            {
                                let created_at = data_item["created_at"]
                                let updated_at = data_item["updated_at"]
                                let default_value = "null" as AnyObject
                                
                                print("cGuid: \(cGuid) \ncName: \(cName) \nbActive: \(bActive) \ncreated_at: \(created_at ?? default_value) \nupdated_at: \(updated_at ?? default_value) \n")
                            }
                        }
                    }
                }
            }
        }
    }
    
    func url_request(completion: @escaping(NSDictionary?) -> ())
    {
        let url_request = URLRequest(url: URL(string: "http://192.168.203.141/")!)
        var arreglo: NSDictionary?
        
        URLSession.shared.dataTask(with: url_request)
        { (data, response, error) in
            if error != nil
            {
                print(error ?? "Error")
            }
            else
            {
                if let url_response = response as? HTTPURLResponse
                {
                    if url_response.statusCode == 200
                    {
                        do
                        {
                            arreglo = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
                        }
                        catch let error as NSError
                        {
                            print(error.localizedDescription)
                        }
                    }
                    else
                    {
                        print(url_response.statusCode)
                    }
                }
            }
            completion(arreglo)
        }.resume()
    }

Realice todo el proceso usando una maquina virtual y funciona :D mi error al obtener datos de NSArray sin antes transformar cada item a [String: AnyObject]

Esto es lo que imprime:

Message: Ok 
isError: false
cGuid: 641f42d7-dfa8-11e7-95da-ec086b0540b2 
cName: Restaurant 
bActive: 1 
created_at: <null> 
updated_at: <null> 

cGuid: 6e71b498-dfa8-11e7-95da-ec086b0540b2 
cName: Restaurant 
bActive: 1 
created_at: <null> 
updated_at: <null> 
Origen Enlace
Angel
  • 262
  • 1
  • 9

En mi caso, lo resolveria de esta manera:

if let message = data["Message"] as? String,
   let isError = data["IsError"] as? Bool,
   let data_data  = data["Data"] as? NSArray
{

    for item in data_data
    {
        if let cGuid = item["cGuid"] as? String,
           let cName = item["cName"] as? String,
           let bActive = item["bActive"] as? Int,
           let created_at = item["created_at"] as? AnyObject,
           let updated_at = item["updated_at"] as? AnyObject
           {
                
           }
    }
}

En cuanto a "created_at" y "updated_at" lo he colocado de esa forma por que no se muy bien lo que estas retornando, si es una fecha un String podria ser una solucion.