swift - 无法转换“Promise”类型的返回表达式'返回类型'承诺' 斯威夫特
问题描述
我是 PromiseKit 的新手,无法获得 Promise 的价值。最终目标是让异步调用解析为 JSON 数组。目前编译器抱怨说:
Cannot convert return expression of type 'Promise<Void>' to return type 'Promise<JSON>'
我很困惑,并且无法找到看起来像我正在做的解决方案。谁能看到我哪里出错了?
这是调用我的后端以获取 Json 数据的 get 方法。
public func get(_ url: URL) -> Promise<JSON> {
return Promise<JSON> { resolver -> Void in
Alamofire.request(url)
.validate()
.responseJSON { response in
switch response.result {
case .success(let json):
let json = JSON()
if let data = response.data {
guard let json = try? JSON(data: data) else {
resolver.reject("Error" as! Error)
return
}
resolver.fulfill(json)
}
case .failure(let error):
resolver.reject(error)
}
}
}
}
这是使用上述方法的方法:
public func fetchAllocations() -> Promise<JSON> {
let url: URL = createAllocationsUrl()
var allocations = JsonArray()
let responsePromise = httpClient.get(url).done { (fetchedJSON) in
let fetchedAlloc: JsonArray = JSON(fetchedJSON).array ?? []
if fetchedAlloc.count > 0 {
let eid = "9b0e33b869"
let previousAlloc = self.store.get(eid)
if let previousAllocations = previousAlloc {
if previousAllocations.count > 0 {
allocations = Allocations.reconcileAllocations(previousAllocations, allocations)
}
}
self.store.set(eid, val: allocations)
self.allocationStatus = AllocationStatus.RETRIEVED
if self.confirmationSandbagged {
self.eventEmitter.confirm(allocations)
}
}
}
return responsePromise
}
在我的视图控制器的按钮中,我正在执行该功能:
// This should be a Promise<JSON> that I can resolve and fulfill
let promise = alloc.fetchAllocations().done { (json) in
self.allocations = [json]
}
let eid = "9b0e33b869"
let cached = store.get(eid)
print("YOUR FETCHED ALLOCATION: \(String(describing: self.allocations))")
print("YOUR CACHED ALLOCATION: \(String(describing: cached))")
当我到达我的打印语句时,我每次都没有。
解决方案
public func fetchAllocations() -> Promise<[JSON]> {
return Promise { resolve in
let url = self.createAllocationsUrl()
let strPromise = HttpClient.get(url: url).done { (stringJSON) in
var allocations = JSON.init(parseJSON: stringJSON).arrayValue
let previous = self.store.get(self.participant.getUserId())
if let prevAlloc = previous {
if Allocator.allocationsNotEmpty(allocations: prevAlloc) {
allocations = Allocations.reconcileAllocations(previousAllocations: prevAlloc, currentAllocations: allocations)
}
}
self.store.set(self.participant.getUserId(), val: allocations)
self.allocationStatus = AllocationStatus.RETRIEVED
if (self.confirmationSandbagged) {
self.eventEmitter.confirm(allocations: allocations)
}
if self.contaminationSandbagged {
self.eventEmitter.contaminate(allocations: allocations)
}
resolve.fulfill(allocations)
do {
try self.execution.executeWithAllocation(rawAllocations: allocations)
} catch let err {
let message = "There was an error executing with allocations. \(err.localizedDescription)"
Log.logger.log(.error, message: message)
}
}
}
}
推荐阅读
- c++ - 如何在 Eclipse 4.7.3a 中抑制“语法错误”
- c# - Get raw decodated name of IDiaSymbol
- sql - 消息表中的复杂 SQL 选择查询
- android - 当我单击线性布局更改背景时,在 Recyclerview 中选择了两个项目
- node.js - Giving resources to a cli user via gulp
- python - 无法以自定义方式生成日期列表
- php - how to write an exact pattern matching in php
- polymer - Use Polymer 2.0 component in Polymer 3.0 app
- android - 活动开始时获取 TabLayout 的第一个选项卡的初始选择事件
- spring-batch - Spring Batch JdbcCursorItemReader moving cursor backward