美文网首页swift学习资集
Swift 自动闭包(autoclosure)

Swift 自动闭包(autoclosure)

作者: sampson666888 | 来源:发表于2021-09-27 13:51 被阅读0次

自动闭包

  • 自动闭包是一种自动创建的闭包,用于包装传递给函数作为参数的表达式。
  • 这种闭包不接受任何参数,当它被调用的时候,会返回被包装在其中的表达式的值。
  • 这种便利语法让你能够省略闭包的花括号,用一个普通的表达式来代替显式的闭包。
  • 自动闭包让你能够延迟求值,因为直到你调用这个闭包,代码段才会被执行。
  • 延迟求值对于那些有副作用(Side Effect)和高计算成本的代码来说是很有益处的,因为它使得你能控制代码的执行时机。
var customersInLine = ["Chris", "Alex", "Ewa", "Barry", "Daniella"]
print(customersInLine.count)    // 打印出“5”
 
let customerProvider = { customersInLine.remove(at: 0) }
print(customersInLine.count)    // 打印出“5”
 
print("Now serving \(customerProvider())!") // Prints "Now serving Chris!"
print(customersInLine.count)    // 打印出“4”

在闭包的代码中,customersInLine 的第一个元素被移除了,不过在闭包被调用之前,这个元素是不会被移除的。
如果这个闭包永远不被调用,那么在闭包里面的表达式将永远不会执行,那意味着列表中的元素永远不会被移除。

注意:
1. 注意,customerProvider 的类型不是 String,而是 () -> String。
2. 一个没有参数且返回值为 String 的函数。

  • 将闭包作为参数传递给函数时,你能获得同样的延时求值行为。
//  serve(customer:) 函数接受一个返回顾客名字的显式的闭包。
func serve(customer customerProvider: () -> String) {
    print("Now serving \(customerProvider())!")
}
serve(customer: { customersInLine.remove(at: 0) } )
// 打印出“Now serving Alex!”
  • 通过将参数标记为 @autoclosure 来接收一个自动闭包。
  • 可以将该函数当作接受 String 类型参数(而非闭包)的函数来调用。
  • customerProvider 参数将自动转化为一个闭包,因为该参数被标记了 @autoclosure 特性。
func serve(customer customerProvider: @autoclosure () -> String) {
    print("Now serving \(customerProvider())!")
}
serve(customer: customersInLine.remove(at: 0))

注意:
1. 过度使用 autoclosures 会让你的代码变得难以理解。
2. 上下文和函数名应该能够清晰地表明求值是被延迟执行的。

如果你想让一个自动闭包可以“逃逸”,则应该同时使用 @autoclosure 和 @escaping 属性。

// customersInLine i= ["Barry", "Daniella"]
var customerProviders: [() -> String] = []
func collectCustomerProviders(_ customerProvider: @autoclosure @escaping () -> String) {
    customerProviders.append(customerProvider)
}
collectCustomerProviders(customersInLine.remove(at: 0))
collectCustomerProviders(customersInLine.remove(at: 0))
 
print("Collected \(customerProviders.count) closures.") // 打印“Collected 2 closures.”
for customerProvider in customerProviders {
    print("Now serving \(customerProvider())!")
}
// 打印“Now serving Barry!”
// 打印“Now serving Daniella!”

collectCustomerProviders(_ : ) 函数并没有调用传入的 customerProvider 闭包,而是将闭包追加到了 customerProviders 数组中。
这个数组定义在函数作用域范围外,这意味着数组内的闭包能够在函数返回之后被调用。
因此,customerProvider 参数必须允许“逃逸”出函数作用域。

相关文章

  • ios 经典面试案例 (六)

    Swift autoclosure的作用? 1:自动闭包,顾名思义是一种自动创建的闭包,用于包装函数参数的表达式,...

  • swift @autoclosure(自动闭包)

    Apple 为了推⼲和介绍 Swift,破天荒地为这⻔语⾔开设了⼀个博客(当然我觉着是因为 Swift 坑太多需要...

  • Swift 关于AUTOCLOSURE 和 ??

    转正:autoclosure:可理解为自动识别的闭包http://swifter.tips/autoclosure/

  • 自动闭包@autoclosure

    自动闭包@autoclosure 什么是自动闭包 自动闭包就是把一个表达式直接自动闭合一个闭包,这样看起来就比较好...

  • @autoclosure(自动闭包)

    Apple 为了推⼲和介绍 Swift,破天荒地为这⻔语⾔开设了⼀个博客(当然我觉着是因为 Swift 坑太多需要...

  • Swift学习 - 初学 @autoclosure

    前言 对于从OC转入Swift的我来说,自动闭包(@autoclosure)是一个新事物,从字面解释看,是可以把方...

  • @autoclosure 和 ??

    自动关闭关键字 @autoclosure 使用与参数类型的()-> T 的闭包 ,这个闭包的参数为空,带有参数的闭...

  • @autoclosure、@noescape和@escape

    @autoclosure:字面意思为自动闭包,该关键字用于在函数或者方法的闭包类型参数前面,但是闭包类型被限定在无...

  • 从??(空合运算符)看autoclosure

    ## 1:autoclosure初识 先看一下书上的解释:自动闭包是一种自动创建的闭包,用于包装传递给函数作为参数...

  • Swift基础知识

    autoclosure @autoclosure 做的事情就是把一句表达式自动地封装成一个闭包。用法就是在类型签名...

网友评论

    本文标题:Swift 自动闭包(autoclosure)

    本文链接:https://www.haomeiwen.com/subject/obtrnltx.html