Cannot convert return expression of type Promise (_,_) -> DataRequest to return type Promise<DataResponse,AnyObject>>

旧时模样 提交于 2019-12-11 05:57:33

问题


Cannot convert return expression of type Promise (,) -> DataRequest to return type Promise>

my function is

func postJson(_ url: String, parameters: [String: String]) -> Promise<DataResponse<AnyObject>> {

        var request = URLRequest(url: URL(string: url)!)
        request.httpMethod = "POST"
        request.setValue("application/json", forHTTPHeaderField: "Content-Type")

        request.httpBody = try! JSONSerialization.data(withJSONObject: parameters)

        return Promise { fulfill, reject in

        manager.request(request)
            .responseJSON { response in
                fulfill(response)
        }

And I get this error on the return Promise line. How do I convert ?

I tried changing my return signature to Promise<DataRequest, Error and get a compile error on that line that Promise is too specialized with 2 parameters instead of 1.


回答1:


Problem is with fulfill because it's expecting parameter DataResponse<AnyObject> but you are passing DataResponse<Any>.

Changing return type of your postJson method to Promise<DataResponse<Any>> should solve your problem.

Change this line

func postJson(_ url: String, parameters: [String: String]) -> Promise<DataResponse<AnyObject>> {

to

func postJson(_ url: String, parameters: [String: String]) -> Promise<DataResponse<Any>> {


来源:https://stackoverflow.com/questions/44640197/cannot-convert-return-expression-of-type-promise-datarequest-to-return

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