Update 07_Closures.md

This commit is contained in:
MagicHu
2015-10-26 15:18:53 +08:00
parent 6e991575b7
commit 55467db948

View File

@ -374,3 +374,23 @@ let alsoIncrementByTen = incrementByTen
alsoIncrementByTen()
// 返回的值为50
```
## 自动闭包Autoclosures
一个自动闭包是一个自动被创建的用于将一个表达式包装为一个参数进而传递到函数的闭包。当它被调用的时候它无需任何的参数然后返回被包装在其中的表达式的值。这种语法糖syntactic convenience让你能够用一个普通的表达式省略围绕在函数参数外的括号来代替显式的闭包。
调用一个闭包作为参数的函数是很常见的,不过,实现那样的函数却不常见。举个例子来说,`assert(condition:message:file:line:)`函数将一个闭包作为它的condition参数和message参数它的condition参数仅仅在编译时被计算求值它的message参数仅当Condition参数为false时被计算求值。
一个自动闭包让你能够延迟计算求值因为代码段不会被执行直到你调用这个闭包。延迟计算求值对于哪些有副作用Side Effect的代码和大量繁杂计算的代码来说是很有益处的因为它让你控制了代码什么时候被运行。下面的代码展示了一个闭包的延时执行。
```swift
var customersInLine = ["Chris", "Alex", "Ewa", "Barry", "Daniella"]
print(customersInLine.count)
// prints "5"
let customerProvider = { customersInLine.removeAtIndex(0) }
print(customersInLine.count)
// prints "5"
print("Now serving \(customerProvider())!")
// prints "Now serving Chris!"
print(customersInLine.count)
// prints "4"
```