无法转换Promise(

编程入门 行业动态 更新时间:2024-10-28 16:18:44
本文介绍了无法转换Promise(_,_)->类型的返回表达式返回类型Promise< DataResponse,AnyObject>>的DataRequest.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

无法将Promise(,)类型的返回表达式转换为DataRequest以返回Promise>类型的

我的功能是

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) }

然后在Promise返回行中收到此错误.如何转换?

我尝试将返回签名更改为Promise<DataRequest, Error,并在该行上看到编译错误,因为Promise过于专门化了2个参数而不是1个参数.

解决方案

fulfill存在问题,因为它需要参数DataResponse<AnyObject>,但是您正在传递DataResponse<Any>.

将postJson方法的返回类型更改为Promise<DataResponse<Any>>应该可以解决您的问题.

更改此行

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

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

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.

解决方案

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>> {

更多推荐

无法转换Promise(

本文发布于:2023-11-26 22:50:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1635528.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Promise

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!