iosswiftalamofirealamofire-request

How to fix the Alamofire 5 error for success and failure cases in swift?


In my project, I am using Alamofire 5.9.3 and below is the uploading the data into the server.

    APISessionController.sharedInstance.sessionManager().upload(multipartFormData: { (multipartFormData) in
            
            for (key, value) in parameters {
                multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
            }
        }, to:url)
        { (result) in
            switch result {
                
            case .success(let upload, _, _): // Getting Error: '_' can only appear in a pattern or on the left side of an assignment
                upload.uploadProgress(closure: { (Progress) in
                        self.delegate.uploadProgress(Progress.fractionCompleted)
                })
                
                upload.responseJSON { response in
                    if let JSON = response.result.value as? Dictionary<String, AnyObject> {
}

            case .failure(_): //Getting Error : '_' can only appear in a pattern or on the left side of an assignment

Previoulsy I was using Alamofire 4.9.1, It was working fine. But it’s showing error in Alamofire 5?


Solution

  • Alamofire's multipart upload APIs changed in Alamofire 5. The encoding completion closure is no longer necessary, you can just use the normal Alamofire APIs.

    APISessionController.sharedInstance.sessionManager().upload(multipartFormData: { (multipartFormData) in
                
                for (key, value) in parameters {
                    multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
                }
            }, to:url)
      .uploadProgress { }
      .response { }