gpt4 book ai didi

ios - 为什么无法编译递归 PromiseKit 函数?

转载 作者:行者123 更新时间:2023-11-28 07:44:58 27 4
gpt4 key购买 nike

我有下一个递归 Promise 函数调用:

 func getPlaces(location: Location) -> Promise<[Geosearch]> {
return Promise().then {
URLSession.shared.dataTask(.promise, with: url)
}.compactMap { result in
let json = try JSONDecoder().decode(JsonPlaces.self, from: result.data)
let places = json.query.geosearch
return places
}
}

func getAtLeastOnePlace(location: Location) -> Promise<[Geosearch]> {
return getPlaces(location).then{ places in //error here
if hasNonVisitedPlaces(places: places) {
return Promise.value(places)
} else {
return getPlaces(location)
}
}
}

func hasNonVisitedPlaces(places: [Geosearch]) -> Bool {
return places.count > 0
}

但是编译器构建失败并出现错误“无法将‘Promise<_.T>’类型的返回表达式转换为‘Promise<[Geosearch]>’返回类型。错误在哪里?


这似乎是编译器错误。此函数已正确编译:

 func getAtLeastOnePlace(location: Location) -> Promise<[Geosearch]> {
return getPlaces(location).then{ places in
return Promise.value(places)
}
}

还有这个功能:

 func getAtLeastOnePlace(location: Location) -> Promise<[Geosearch]> {
return getPlaces(location).then{ places in
return getPlaces(location)
}
}

但是这个函数导致了一个错误:

 func getAtLeastOnePlace(location: Location) -> Promise<[Geosearch]> {
return getPlaces(location).then{ places in //error here
if hasNonVisitedPlaces(places: places) {
return Promise.value(places)
} else {
return getPlaces(location)
}
}
}

最佳答案

有时我们需要显式提供返回类型,因此请尝试以下操作。

func getAtLeastOnePlace(location: Location) -> Promise<[Geosearch]> {
return getPlaces(location).then{ places -> Promise<[Geosearch]> in
if hasNonVisitedPlaces(places: places) {
return Promise.value(places)
} else {
return getPlaces(location)
}
}
}

关于ios - 为什么无法编译递归 PromiseKit 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51341657/

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