无法转换Promise(_,_)->类型的返回表达式返回类型Promise< DataResponse,AnyObject>>的DataRequest. [英] Cannot convert return expression of type Promise (_,_) -> DataRequest to return type Promise<DataResponse,AnyObject>>
问题描述
无法将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(_,_)->类型的返回表达式返回类型Promise< DataResponse,AnyObject>>的DataRequest.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!