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/[email protected]','https://s3-sa-east-1.amazonaws.com/pipow/categorias/icones/[email protected]','https://s3-sa-east-1.amazonaws.com/pipow/categorias/icones/[email protected]']
2
  • are you using swiftyjson? that will make it much easier to do. Commented Nov 29, 2017 at 4: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. Commented Nov 29, 2017 at 4:13

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.