gpt4 book ai didi

swift - 无法将类型 'Promise' 的返回表达式转换为返回类型 'Promise' Swift

转载 作者:行者123 更新时间:2023-11-30 10:41:11 25 4
gpt4 key购买 nike

我是 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
}

在我的 View Controller 的按钮中,我正在执行该函数:

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

关于swift - 无法将类型 'Promise<Void>' 的返回表达式转换为返回类型 'Promise<JSON>' Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56763077/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com