> For the complete documentation index, see [llms.txt](https://gitbook.existorlive.cn/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gitbook.existorlive.cn/kai-fa-yu-yan-xue-xi/swift/7.-bi-bao/7.-zi-dong-bi-bao.md).

# 自动闭包

**自动闭包**是一种自动创建的闭包，用于包装传递给函数作为参数的表达式。

这种闭包**不接受任何参数**，当它被调用的时候，会返回被包装在其中的表达式的值。

在声明时，需要`@autoclosure`指定自动闭包

```swift
// customersInLine is ["Ewa", "Barry", "Daniella"]
func serve(customer customerProvider: @autoclosure () -> String) {
    print("Now serving \(customerProvider())!")
}
serve(customer: customersInLine.remove(at: 0))
```

> 过度使用 autoclosures 会让你的代码变得难以理解
