3

I have used Alamofire in one of my application. Now Application needs to update. So I have updated Alamofire pods with new version.

Now I don't know how to call Alamofire request in swift 5 with new version of Alamofire. I have tried, but it's throwing error. I have searched lot, but not found ny solution.

Can anyone help me to make a call with Alamofire?

Errors :

  1. Protocol type 'Any' cannot conform to 'Encodable' because only concrete types can conform to protocols
  2. Value of type 'Result' has no member 'isFailure'
  3. Value of type 'Result' has no member 'error'

A

VRAwesome
  • 4,721
  • 5
  • 27
  • 52

1 Answers1

3

I have created a sample app just to give an idea

struct Article: Codable {
    let firstName: String
    let lastName: String
    let email: String
}

struct User: Encodable {
    let name: String
    let password: String
}
// data model
let userStruct = User(name: "test", password: "pass")

// data dictionary
let userDictionary = ["name": "test", "password": "pass"]

func getData() {
        let headers: HTTPHeaders = ["Access-Token-Key": ""]
        let request = AF.request(url,
                   method: .post,
                   parameters: userDictionary,   // or 'userStruct' because both conform to 'Encodable' parameters.
                   encoder: JSONParameterEncoder.default,
                   headers: headers)
        // Receive and decode the server's JSON response.
        request.responseDecodable(of: Article.self) { response in
           switch response.result {
           case let .success(result):
              // the decoded result of type 'Article' that you received from the server.
            print("Result is: \(result)")
           case let .failure(error):
              // Handle the error.
            print("Error description is: \(error.localizedDescription)")
           }
        }
    }

If you want to use parameters of type [String: Any] then use Alamofire version 4.9.1. The version which I used in the sample app is 5.0.0-rc.3

Rachit Agarwal
  • 356
  • 2
  • 9