1

I getting the result as an array of strings like this

["India","America","Australia","China","Russia"]

And I'm using Alamofire to get the response using code. There's no error, but I got the result as null. Please help in parsing this.

sessionManager?.request(strURL, method: method, parameters: params, encoding: encoding , headers: headers).responseJSON { (response) in
   switch response.result {
        case .success:
            let resJson = JSON(response.result.value!)
           success(resJson)
            break
        case .failure(let error):
            failure(error as NSError)
            break
        }

    }
Roham Rafii
  • 2,929
  • 7
  • 35
  • 49
Jai Deves
  • 82
  • 9

3 Answers3

4

Try this:

if let responseData = response.result.value{    
let responsevalue = responseData  as? [String]
}
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
Anuraj
  • 1,242
  • 10
  • 25
1

For anyone looking for another derived answer, just put this chunk of code after Alamofire.request(...):

.responseJSON(completionHandler: { (response) in

            switch response.result{

            case .success(let value):

                // Here is your array of String
                let arrayOfStrings = value as? [String]

            case .failure(let error):

                // Some code when error happens...
                print(error.localizedDescription)

            }

        })
Burak
  • 525
  • 4
  • 24
0

This solution using SwiftyJSON:

.responseJSON(completionHandler: { (response) in

     switch response.result{
     case .failure(let error):
         print(error.localizedDescription)

     case .success(let res):
       let json = JSON(res)
       let res = json["result"]

       var models = [String]()
       if let models1 = company["models"].array {
           for model in models1 {
               guard let mod = model.string else { return }
               models.append(mod)
           }
       }
     }
})
Alirza Eram
  • 134
  • 11