Siva Sankar
Siva Sankar

Reputation: 543

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;
})

Upvotes: 7

Views: 17647

Answers (3)

unixeo
unixeo

Reputation: 1186

For Alamo 5 you have to use:

response.value

Upvotes: 20

Vladlex
Vladlex

Reputation: 875

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
                }

Upvotes: 16

Bikram Aryal
Bikram Aryal

Reputation: 46

The error Value of type 'Result' has no member 'value' is due to Alamofire version 5, in version 4.8.2 Result is of type Result of type < Any>

If you have installed Alamofire through pod, then you should get version 4.8.2 where your code works fine.

Alamofire.request("https://jsonplaceholder.typicode.com/todos/1", method: .get)
        .responseJSON { (response) in

            switch response.result {
            case .success(_):
                if let JSON = response.result.value as? [String: Any] {
                    let status = JSON["completed"] as! Bool
                    print(status)
                }



            case .failure(_): break

            }
    }

In this case Result< Any> type not Result< Any, Error> And use Alamofire instead of AF

Upvotes: 0

Related Questions