有没有一种方法可以运行一个函数,如果一个守卫站在返回之前没有得到满足?
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个。
你可以回来 functionToRun 如果函数包含 guard 语句的返回类型与 功能运行 . 例子:
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") } } }