【发布时间】:2019-08-14 00:11:57
【问题描述】:
我正在尝试关注example in the Swift docs 进行尾随关闭。
这是函数:
func someFunctionThatTakesAClosure(closure: () -> Void) {
// function body goes here
print("we do something here and then go back")//does not print
}
我在这里称呼它。
print("about to call function")//prints ok
someFunctionThatTakesAClosure(closure: {
print("we did what was in the function and can now do something else")//does not print
})
print("after calling function")//prints ok
但是,该函数没有被调用。以上有什么问题?
这是苹果的例子:
func someFunctionThatTakesAClosure(closure: () -> Void) { // 函数体放在这里 }
// 下面是如何在不使用尾随闭包的情况下调用此函数:
someFunctionThatTakesAClosure(闭包:{ // 闭包的主体放在这里 })
【问题讨论】:
-
闭包只是一个值。就像任何其他值一样,您可以“忽略”它的存在并且什么都不会发生。让任何事情发生都需要您实际使用该值。
标签: ios swift closures trailing