gpt4 book ai didi

ios - 从DispatchQueue Swift内部返回

转载 作者:行者123 更新时间:2023-12-01 19:33:48 26 4
gpt4 key购买 nike

我正在构建一个函数,该函数接受CLLocation参数,并返回我自己定义的DarkSkyResponse类型的天气预报。

public func getForecast(at location: CLLocation) -> DarkSkyResponse {
let request = DarkSkyRequest(key: "KEYGOESHERE")
let point = DarkSkyRequest.Point(location.coordinate.latitude, location.coordinate.longitude)

guard let url = request.buildURL(point: point) else {
preconditionFailure("Failed to construct URL")
}

let task = URLSession.shared.dataTask(with: url) {
data, response, error in

DispatchQueue.main.async {
guard let data = data else {
fatalError("No Data Recieved")
}
guard let forecast = DarkSkyResponse(data: data) else {
fatalError("Decoding Failed")
}
return forecast
}
}
task.resume()
}

一切工作正常,我只想返回在DispatchQueue中找到的 forecast变量。当我尝试执行此操作时,遇到以下错误: Unexpected non-void return value in void function我相信这是因为我要返回DispatchQueue内部。

为了解决这个问题,我将数据任务和解码过程移入了具有完成处理程序的函数中。当我调用该函数时,只需将闭包设置为按以下方式返回输出:
public func getForecast(at location: CLLocation) -> DarkSkyResponse {
let request = DarkSkyRequest(key: "KEYGOESHERE")
let point = DarkSkyRequest.Point(location.coordinate.latitude, location.coordinate.longitude)

guard let url = request.buildURL(point: point) else {
preconditionFailure("Failed to construct URL")
}

func getDataAndDecode(from url: URL, completionBlock: @escaping (DarkSkyResponse) -> Void) -> Void {
let task = URLSession.shared.dataTask(with: url) {
data, response, error in

DispatchQueue.main.async {
guard let data = data else {
fatalError("No Data Recieved")
}
guard let forecast = DarkSkyResponse(data: data) else {
fatalError("Decoding Failed")
}
completionBlock(forecast)
}
}
task.resume()
}

getDataAndDecode(from: url) { (output) in
return output
}
}

现在我得到一个警告: Expression of type 'DarkSkyResponse' is unused。我正在以正确的方式执行此操作,还是有一种更优雅的方式来返回我想要的值?

最佳答案

到目前为止,swift尚不支持异步等待样式(类似于javascript),这意味着您无法直接从异步操作中返回。
最好将callback(completionBlock)参数放在getForecast方法上。如果回调函数也处理错误的情况(例如onSuccess和onError),那就太好了。并且不要忘记将getForecast方法设置为void方法。

因此,就像这样:


func getForecast(at location: CLLocation, onSuccess: (DarkSkyResponse) -> (), onError: (Error) -> ()) {

...
}

关于ios - 从DispatchQueue Swift内部返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60957291/

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