代码之家  ›  专栏  ›  技术社区  ›  Tony Merritt

在点击返回之前运行一个函数。iOS,斯威夫特,

  •  0
  • Tony Merritt  · 技术社区  · 7 年前

    有没有一种方法可以运行一个函数,如果一个守卫站在返回之前没有得到满足?

    guard let detectedRectangle = observations.first else { 
    
      functionToRun(completion: { (complete) in 
    
          print("Tony GOT TO RETURN"); return }
        })
    

    上面的代码使我将返回部分放在完成之外。

    guard let detectedRectangle = observations.first else { 
    
      functionToRun(completion: { (complete) in 
    
    
        })
    
    print("Tony GOT TO RETURN"); return } 
    

    我在函数中设置了3个打印语句,在某些点上运行,在打返回打印之前,它将到达其中的2个。

    1 回复  |  直到 7 年前
        1
  •  1
  •   pacification    7 年前

    你可以回来 functionToRun 如果函数包含 guard 语句的返回类型与 功能运行 . 例子:

    func guardHolder() { // guardHolder returns Void
        guard let detectedRectangle = observations.first else {
            // so, if functionToRun also returns Void type, we can return this func
            return functionToRun { _ in print("Tony GOT TO RETURN") }
        }
    }
    
    推荐文章