使用Alamofire的Void错误和使用Swift的PromiseKit的错误(请求)

时间:2019-06-21 16:19:12

标签: swift xcode alamofire promisekit alamofire-request

在我的上一个项目中,我一直在使用alamofire和promisekit连接到我的Web服务。现在,我都拥有一个新版本,并且在创建请求时遇到此错误

Cannot convert value of type 'DataRequest' to closure result type 'Void'

当我尝试这样做时会发生这种情况:

func CallOne() -> Promise<[String: AnyObject]>{
        return Promise{
            fullfil,reject -> Void in
            return Alamofire.request(
                url_api,
                method: .get).responseJSON{
                    response in
                    switch(response.result){
                    case .success(let res):
                        let data = res as? [String: AnyObject]
                        fullfil(data!)
                    case .failure(let error):
                        reject(error)
                    }
            }
        }
    }

以及当我尝试使用此功能时:

func CallTwo(email: String, phone: String) -> Promise<[String: AnyObject]>{
    let params =
        ["email": email, "phone": phone]
    return Promise{
        fullfil,reject -> Void in
        return Alamofire.upload(
            multipartFormData: { MultipartFormData in
                for (key, value) in params {
                    MultipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
                }
        }, to: url_api, method: .post) { (result) in
            switch (result){
            case .success(let upload, _, _):
                upload.responseJSON { response in
                    switch(response.result){
                    case .success(let res):
                        let data = res as? [String: AnyObject]
                        fullfil(data!)
                    case .failure(let error):
                        reject(error)
                    }
                }
            case .failure(let encodingError):
                print(encodingError)
            }
        }
    }
}

我收到此错误Unable to infer closure type in the current context

我该如何解决?预先感谢

1 个答案:

答案 0 :(得分:0)

我使用

解决此问题
  

盖章

func CallOne() -> Promise<[String: AnyObject]>{
    return Promise{
        seal in
        return Alamofire.request(
            url_api,
            method: .get).responseJSON{
                response in
                switch(response.result){
                case .success(let res):
                    let data = res as? [String: AnyObject]
                    seal.fulfill(data!)
                case .failure(let error):
                    seal.reject(error)
                }
        }
    }
}

func CallTwo(email: String, phone: String) -> Promise<[String: AnyObject]>{
    let params =
        ["email": email, "phone": phone]
    return Promise{
        seal in
        return Alamofire.upload(
            multipartFormData: { MultipartFormData in
                for (key, value) in params {
                    MultipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
                }
        }, to: url_api, method: .post) { (result) in
            switch (result){
            case .success(let upload, _, _):
                upload.responseJSON { response in
                    switch(response.result){
                    case .success(let res):
                        let data = res as? [String: AnyObject]
                        seal.fulfill(data!)
                    case .failure(let error):
                        seal.reject(error)
                    }
                }
            case .failure(let encodingError):
                print(encodingError)
            }
        }
    }
}