gpt4 book ai didi

swift - 快速传递一个元素

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

我遇到了一些问题并且真的迷失了所以这是最后的手段。

我有这个功能:

func fetchGoogleData(forLocation: CLLocation) {
//guard let location = currentLocation else { return }
googleClient.getGooglePlacesData(location: forLocation) { (response) in
self.listResults(places: response.results)
}
}

调用此函数:

func listResults(places: [Place]) {
dump(places)
}

但是,转储 responses.resultplaces 会返回空值,但循环遍历 places 会返回所有正确的值。

有没有办法在不调用此函数的情况下返回 response.results 作为值?

最佳答案

您可以使用完成 block (Closures)返回您的结果。假设您的 response.results 类型为 [Place]。那么你的函数应该如下所示

func fetchGoogleData(forLocation: CLLocation, completion: @escaping [Place]->Void) {
//guard let location = currentLocation else { return }
googleClient.getGooglePlacesData(location: forLocation) { (response) in
completion(response.results)
}
}

并按如下方式调用它:

self.fetchGoogleData(forLocation: currentPosition) { (places) in
//print(places)
}

关于swift - 快速传递一个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54878865/

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