swift-自动闭包 官网
Swift 里的自动闭包是一种自动创建的闭包,它把表达式封装成闭包,不接收参数,调用时返回封装的表达式值。 var customersInLine = ["Chris", "Alex", "Ewa", "Barry", "Daniella"]print(customersInLine.count) // 输出 5let customerProvider = { customersInLine.remove(at: 0) }print(customersInLine.count) // 输出 5print("Now serving \(customerProvider())!") // 输出 "Now serving Chris!"print(customersInLine.count) // 输出 4 自动闭包可延迟求值,直到调用它。 func serve(customer customerProvider: @autoclosure () -> String) { print("Now serving \(customerProvider())!")}serve(customer: customersInLine.remove(at: 0)) 使用自动闭包时,要留意延迟求值可能带来的副作用。