Search code examples
iosswiftafnetworking-2alamofire

Parse error in Alamofire in swift


I am getting this response.

SUCCESS: {
    msg = "Login successfully";
    "type_id" = 1;
    "user_id" = 2;
} 

I used this code.

Alamofire.request(.POST, "http:/www.abc.com" , parameters: ["consumer_key": "Og5pRGI2V"]).responseJSON { response in

                    print(response)
                   let employees = response["employees"]! as NSDictionary //getting error type response has no subscript member
                }

Which is type of response ? How to parse this "msg","user_id" ?. thanks in advance.


Solution

  • Alamofire parses the response data and stores it in the response object. You can get it like this:

    Alamofire.request(.POST, "http:/www.abc.com" , parameters: ["consumer_key": "Og5pRGI2V"]).responseJSON { response in
        print(response)
        if let dict = response.result.value {
            let msg = dict["msg"]
            let userId = dict["user_id"]
            // Do things with the values
        }
    }