【问题标题】:How to escape (to initial calling place) from a recursive function in swift?如何快速从递归函数中逃脱(到初始调用位置)?
【发布时间】:2021-09-20 08:39:36
【问题描述】:
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
    }
}

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

【问题讨论】:

    标签: ios swift recursion escaping closures


    【解决方案1】:

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

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

    myEscapingRecursiveFunction("xyz", done: done)
    

    【讨论】:

      猜你喜欢
      • 2015-10-20
      • 1970-01-01
      • 1970-01-01
      • 2012-03-11
      • 1970-01-01
      • 1970-01-01
      • 2020-12-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多