我想理解快速关闭。我有以下迅速实施:Swift:理解迅速关闭
func whereToGo (ahead:Bool) -> (Int) -> Int{
func goAhead(input:Int) ->Int{
return input + 1 }
func goBack(input:Int) ->Int{
return input - 1 }
return ahead ? goAhead : goBack
}
var stepsToHome = -10
let goHome = whereToGo(ahead: stepsToHome < 0)
while stepsToHome != 0 {
print("steps to home: \(abs(stepsToHome))")
stepsToHome = goHome(stepsToHome)
}
执行的输出如下:
steps to home: 10
steps to home: 9
steps to home: 8
steps to home: 7
steps to home: 6
steps to home: 5
steps to home: 4
steps to home: 3
steps to home: 2
steps to home: 1
我的问题有以下几点:
为什么只执行该关闭:
func goAhead(input:Int) ->Int{ return input + 1 }
为什么在这条线上没有采取变量值:
提前回来? goAhead:goBack
我真的很感谢您的帮助,了解如何迅速关闭工作。
您的问题更多地讨论嵌套函数,这是闭包的特例。 –
正如苹果文档所说的 - :嵌套函数是具有名称的闭包,并且可以从其封闭函数捕获值.https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Closures.html –