我需要一个按需执行的闭包方法。
在Swift 3中我曾经做过类似的事情:
// Declare closure
var checksPerformed: ((Void) -> Void)? // Declaration
// Call when needed
checksPerformed?()
//Only executes when checksPerformed is called
checksPerformed = { _ in
// do stuff here
}
在Swift 4中,情况已不再如此。
修好此警告后,没有任何作用。 这样做的新方法是什么?
如果我要将声明更新为:var checksPerformed: (() -> ())?
我正在
答案 0 :(得分:4)
Distinguish between single-tuple and multiple-argument function types
// Declare closure
var checksPerformed: (() -> ())? // Declaration
// Call when needed
checksPerformed?()
//Only executes when checksPerformed is called
checksPerformed = {
// do stuff here
}
答案 1 :(得分:0)
使用
var checksPerformed: (()->(Void))?
如果你这样做,一切都应该按预期工作。
在分配闭包时不要使用_ in:
//Only executes when checksPerformed is called
checksPerformed = {
// do stuff here
}
//Call when needed
checksPerformed?()