Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get and save the response in swift5 and Alamofire 5 beta version?

I tried some code but still, my issue is not resolved. Please help me i am new in swift code.

let parameters: Parameters = ["skey": "XXXXXX","country_code":"91","mobile":"XXX004","user_role":"4"]


 AF.request("http://XXXXX/dev/clinic/api/v1/login_otp?", method: .get, parameters: parameters)
    .responseJSON { (response) in
        switch response.result {
        case .success:
            if let JSON = response.result.value as? [String: Any] {
                let status = JSON["status"] as! String
                print(status)
            }
        case .failure(let error): break
            // error handling
        }
}

bellow is the server responce

 success({
      message = "Otp sent successfully on +9170XXXX1004";
      status = 1;
})
like image 825
Siva Sankar Avatar asked Jul 18 '19 06:07

Siva Sankar


People also ask

How do I get Alamofire data?

Follow the steps given in the previous chapter to install Alamofire in the application and open the xcworkspace file. Create a MainViewController class and make a get request to print the API data using Alamofire. // Do any additional setup after loading the view. print("Response.

Does Alamofire use URLSession?

Alamofire is built on top of NSURLSession, and is less lines of code to do REST interactions with (POST/GET/PUT/etc). It will get you "90%" of the way, but if you need to do super specialized network calls, you will need to use NSURLSession.


2 Answers

For Alamo 5 you have to use:

response.value
like image 161
unixeO Avatar answered Nov 15 '22 08:11

unixeO


Try the following code, please:

         switch response.result {
                case .success(let value):
                    if let JSON = value as? [String: Any] {
                        let status = JSON["status"] as! String
                        print(status)
                    }
                case .failure(let error): break
                    // error handling
                }

like image 45
Vladlex Avatar answered Nov 15 '22 08:11

Vladlex