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

▼魔方 西西 提交于 2020-02-03 10:44:08

问题


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://chkdin.biz/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 +917013001004";
      status = 1;
})

回答1:


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
                }




回答2:


For Alamo 5 you have to use:

response.value



回答3:


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



来源:https://stackoverflow.com/questions/57088201/how-to-get-and-save-the-response-in-swift5-and-alamofire-5-beta-version

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!