如何从 swift 中的递归函数转义(到初始调用位置)?
How to escape (to initial calling place) from a recursive function in swift?
var currentCount = 0
let totalCount = 100
func myEscapingRecursiveFunction(_ json: String, done: @escaping (Bool) -> Void) {
currentCount+=1
if currentCount == totalCount {
done(true)
}
else {
myEscapingRecursiveFunction("xyz") { done in
// what should I do here????
}
}
通话中
// (Initially called here)
myEscapingRecursiveFunction("xyz") { done in
if done {
print("completed") // I want to get response here after the recursion is finished
}
}
我希望我的函数仅在当前计数等于总计数时转义,否则它应该递归,问题是我想在它所在的地方得到响应最初被称为,但它总是会在上次调用它的地方执行完成处理程序代码。这里:
您只需将相同的转义块传递给递归函数即可。
所以这样调用你的函数。
myEscapingRecursiveFunction("xyz", done: done)
var currentCount = 0
let totalCount = 100
func myEscapingRecursiveFunction(_ json: String, done: @escaping (Bool) -> Void) {
currentCount+=1
if currentCount == totalCount {
done(true)
}
else {
myEscapingRecursiveFunction("xyz") { done in
// what should I do here????
}
}
通话中
// (Initially called here)
myEscapingRecursiveFunction("xyz") { done in
if done {
print("completed") // I want to get response here after the recursion is finished
}
}
我希望我的函数仅在当前计数等于总计数时转义,否则它应该递归,问题是我想在它所在的地方得到响应最初被称为,但它总是会在上次调用它的地方执行完成处理程序代码。这里:
您只需将相同的转义块传递给递归函数即可。
所以这样调用你的函数。
myEscapingRecursiveFunction("xyz", done: done)