Error Alamofire 5响应成功和响应失败

问题描述 投票:1回答:2

我在版本4中使用Alamofire,并且运行良好。当我将Alamofire更新到版本5时,它给我一个错误,得到结果响应“ .success”或“ .failure”

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9hS0RvdS5wbmcifQ==” alt =“在此处输入图像描述”>

如何使用Alamofire 5获得结果响应“ .success”或“ .failure”?

AF.upload(multipartFormData: { multipartFormData in

            multipartFormData.append(imgData!, withName: "signature", fileName: String("signature"), mimeType: "image/jpg")

            for (key, value) in parameters {
                multipartFormData.append((value.data(using: String.Encoding.utf8)!), withName: key)
            }
        },
        to: URL,
        method: .post,
        headers: headers) { (result) in
            switch result {
            case .success(let upload, _, _):

                upload.uploadProgress(closure: { (progress) in
                    print("Upload Progress: \(progress.fractionCompleted)")
                })

                upload.responseJSON { response in
                    print("Succes: ", response.result.value ?? "")
                    if let data = response.data {
                        do {
                            let decoder = JSONDecoder()
                            let model = try decoder.decode(T.self, from: data)
                            success(model)
                        } catch let error as NSError {
                            print(error)
                        }
                    }
                }
            case .failure(let encodingError):
                print("error: ", encodingError)
            }
        }
ios swift alamofire
2个回答
0
投票

在Alamofire 5.0中,为什么需要成功和失败?我通过下面的代码实现它

  static func uploadFile(url: String, fileData: Data?, parameters: [String : String], type: MessageType!, fileName: String!, completionHandler: @escaping (Result<Data, Error>) -> Void) {

        _ = AF.upload(multipartFormData: { (multiFormData) in
            var fileType: String!
            if type! == MessageType.Video {
                fileType = "video/mp4"
            } else if type! ==  MessageType.Image {
                fileType = "image/jpeg"
            }
            multiFormData.append(fileData!, withName: "uploadedfile", fileName: fileName , mimeType: fileType)
            for (key, value) in parameters {
                multiFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
            }
        }, to: url).uploadProgress(queue: .main, closure: { progress in
            print("Upload Progress: \(progress.fractionCompleted)")
        }).responseJSON(completionHandler: { response in
            if response.error != nil {
                completionHandler(.failure(response.error!))
            } else {
                completionHandler(.success(response.data!))
            }
        })



    }

0
投票

几天前我遇到了同样的问题,这是该代码的有效代码。

pod'Alamofire','5.1.0'

    AF.upload(multipartFormData: { (mulipartFormData) in
        for (key, value) in parameters {
            mulipartFormData.append(value.data(using: .utf8)!, withName: key)
        }
        for (image, key) in images {
            let imgData = image.jpegData(compressionQuality: compression)!
            mulipartFormData.append(imgData, withName: key, fileName: "\(key).jpeg", mimeType: "image/jpeg")
        }

    }, to: url, method: .post, headers:  HTTPHeaders(headers)).response { response in

        onComplete(response.data, response.response, response.error)

    }.uploadProgress { progress in

        print("progress: \(progress.fractionCompleted * 100)")
        onProgress?(progress)
    }
© www.soinside.com 2019 - 2024. All rights reserved.