PromiseKit 无法使用“(Data)”类型的参数列表调用 'fulfill'
PromiseKit Cannot invoke 'fulfill' with an argument list of type '(Data)'
我试图在下面的代码中使用 PromiseKit 从网络请求中获取数据,但是在 seal.fulfill(data)
行我收到错误消息“无法使用参数调用 'fulfill'” “(数据)”类型的列表。谁能告诉我我做错了什么?
import Foundation
import PromiseKit
class NetworkService {
let baseHost: String = "api.someAwesomeSite.com"
var version: String { return "3.345" }
let configuration = URLSessionConfiguration.default
func getData<Data>(method: String, queryItems: [URLQueryItem] = []) -> Promise<Data> {
var urlConstructor = URLComponents()
urlConstructor.scheme = "https"
urlConstructor.host = self.baseHost
urlConstructor.path = "/method/\(method)"
urlConstructor.queryItems = [
URLQueryItem(name: "user_id", value: ApiManager.session.userId),
URLQueryItem(name: "access_token", value: ApiManager.session.token),
URLQueryItem(name: "v", value: version),
]
urlConstructor.queryItems?.append(contentsOf: queryItems)
let request = URLRequest(url: urlConstructor.url!)
let session = URLSession(configuration: configuration)
return Promise<Data> { seal in
let task = session.dataTask(with: request) { (data, response, apiError) in
if let error = apiError {
seal.reject(error)
}
if let data = data {
seal.fulfill(data)
}
}
task.resume()
}
}
我认为您只是对您的通用参数有点困惑。变化:
func getData<Data>(method: String, queryItems: [URLQueryItem] = []) -> Promise<Data> {
至
func getData(method: String, queryItems: [URLQueryItem] = []) -> Promise<Data> {
问题是,当您将 Data
作为通用类型参数时,编译器认为 Data
是您将定义的某种类型...但在函数中您还使用了 Data
来引用 Swift 库 Data
类型,它不能同时是两者。
我试图在下面的代码中使用 PromiseKit 从网络请求中获取数据,但是在 seal.fulfill(data)
行我收到错误消息“无法使用参数调用 'fulfill'” “(数据)”类型的列表。谁能告诉我我做错了什么?
import Foundation
import PromiseKit
class NetworkService {
let baseHost: String = "api.someAwesomeSite.com"
var version: String { return "3.345" }
let configuration = URLSessionConfiguration.default
func getData<Data>(method: String, queryItems: [URLQueryItem] = []) -> Promise<Data> {
var urlConstructor = URLComponents()
urlConstructor.scheme = "https"
urlConstructor.host = self.baseHost
urlConstructor.path = "/method/\(method)"
urlConstructor.queryItems = [
URLQueryItem(name: "user_id", value: ApiManager.session.userId),
URLQueryItem(name: "access_token", value: ApiManager.session.token),
URLQueryItem(name: "v", value: version),
]
urlConstructor.queryItems?.append(contentsOf: queryItems)
let request = URLRequest(url: urlConstructor.url!)
let session = URLSession(configuration: configuration)
return Promise<Data> { seal in
let task = session.dataTask(with: request) { (data, response, apiError) in
if let error = apiError {
seal.reject(error)
}
if let data = data {
seal.fulfill(data)
}
}
task.resume()
}
}
我认为您只是对您的通用参数有点困惑。变化:
func getData<Data>(method: String, queryItems: [URLQueryItem] = []) -> Promise<Data> {
至
func getData(method: String, queryItems: [URLQueryItem] = []) -> Promise<Data> {
问题是,当您将 Data
作为通用类型参数时,编译器认为 Data
是您将定义的某种类型...但在函数中您还使用了 Data
来引用 Swift 库 Data
类型,它不能同时是两者。