0

I need to parse data from this url ( https://fierce-wildwood-95045.herokuapp.com/categoria ) in 4 different arrays, one containing the name, one containing the image url and other containing its description.

It should look like this:

nameArray = ['Iluminação','Acessibilidade','Segurança','Sinalização']
descriptionArray = ['Problemas com iluminação pública como postes com lâmpadas queimadas','Problemas na infraestrutura de acessibilidade como calçadas impróprias','Problemas de segurança como falta de policiamento','Problemas de sinalização como placas quebradas ou pichadas']
imageArray = ['https://s3-sa-east-1.amazonaws.com/pipow/categorias/icones/IluBT%403x.png','https://s3-sa-east-1.amazonaws.com/pipow/categorias/icones/AccessBT@3x.png','https://s3-sa-east-1.amazonaws.com/pipow/categorias/icones/SegBT@3x.png','https://s3-sa-east-1.amazonaws.com/pipow/categorias/icones/SinBT@3x.png']
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
therap76
  • 9
  • 2
  • are you using swiftyjson? that will make it much easier to do. – Nevin Jethmalani Nov 29 '17 at 04:09
  • 1
    Is there any particular reason that you want 4 arrays rather than a single array of a suitable struct? You can use `Codable` to create that array. Also, please edit your question to show the code you have tried and what isn't working or the problems you are having. – Paulw11 Nov 29 '17 at 04:13

1 Answers1

1

You can get result with Alamofire like this:

Alamofire.request("https://fierce-wildwood-95045.herokuapp.com/categoria", method: .get, parameters: nil, encoding: URLEncoding.default, headers: [:])
        .responseJSON { respone in
            let response_array = respone.result.value as! NSArray
            var id_array : [NSDictionary] = []
            var name_array : [String] = []
            var description_array : [String] = []
            var image_array : [String] = []
            for i in 0..<response_array.count
            {
                id_array.append(((response_array[i] as! NSDictionary).value(forKey: "_id") as! NSDictionary))
                name_array.append(((response_array[i] as! NSDictionary).value(forKey: "nome") as! String))
                description_array.append(((response_array[i] as! NSDictionary).value(forKey: "descricao") as! String))
                image_array.append(((response_array[i] as! NSDictionary).value(forKey: "urlImagem") as! String))
            }
            print("id = \(id_array)")
            print("name = \(name_array)")
            print("description = \(description_array)")
            print("image = \(image_array)")
    }

output output description

Rohan Dave
  • 251
  • 1
  • 7
  • Why `NSArray` / `NSDictionary` rather than Swift native collection types?. Why `valueForKey` rather than key subscription? Why `Alamofire` rather than `URLSession`? Why ugly index-based loop rather than Fast Enumeration? Why calling `(response_array[i] as! NSDictionary)` 4 times to get the same object again and again rather than a temporary variable (not even needed with Fast Enumeration)? Why snake_case variable names rather then camelCase? – vadian Nov 29 '17 at 14:28