如何快速从递归函数中转义到初始调用位置?

问题描述

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

我希望我的函数仅在当前计数等于总计数时转义,否则它应该递归,问题是我想在最初调用它的地方得到响应,但它将始终执行上次调用的完成处理程序代码。此处:

enter image description here

解决方法

您只需要将相同的转义块传递给您的递归函数。

所以像这样调用你的函数。

myEscapingRecursiveFunction("xyz",done: done)