在 Swift 中,(()->()) 和 @escaping() -> 有什么区别?空白? [英] In Swift, What is the difference between (()->()) and @escaping () -> Void?

查看:82
本文介绍了在 Swift 中,(()->()) 和 @escaping() -> 有什么区别?空白?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(()->()) 和@escaping() -> Void 有什么区别?

What is the difference between (()->()) and @escaping () -> Void?

func foo(_ completion: (() -> ()) { }

func boo(_ completion: @escaping () -> Void) { }

推荐答案

快速摘录

<小时>

转义闭包
当闭包作为参数传递给函数时,闭包被称为对函数进行转义,但在函数返回后被调用.当您声明一个将闭包作为其参数之一的函数时,您可以在参数类型前写上 @escaping 以指示允许闭包转义.

Quick Excerpt


Escaping Closures
A closure is said to escape a function when the closure is passed as an argument to the function, but is called after the function returns. When you declare a function that takes a closure as one of its parameters, you can write @escaping before the parameter’s type to indicate that the closure is allowed to escape.

闭包可以转义的一种方法是存储在函数外部定义的变量中.例如,许多启动异步操作的函数将闭包参数作为完成处理程序.函数在开始操作后返回,但直到操作完成才调用闭包——闭包需要转义,稍后再调用.例如:

One way that a closure can escape is by being stored in a variable that is defined outside the function. As an example, many functions that start an asynchronous operation take a closure argument as a completion handler. The function returns after it starts the operation, but the closure isn’t called until the operation is completed—the closure needs to escape, to be called later. For example:

var completionHandlers: [() -> Void] = []
func someFunctionWithEscapingClosure(completionHandler: @escaping () -> Void) {
    completionHandlers.append(completionHandler)
}

someFunctionWithEscapingClosure(_:) 函数接受一个闭包作为它的参数,并将它添加到一个在函数外部声明的数组中.如果你没有用@escaping标记这个函数的参数,你会得到一个编译时错误."

The someFunctionWithEscapingClosure(_:) function takes a closure as its argument and adds it to an array that’s declared outside the function. If you didn’t mark the parameter of this function with @escaping, you would get a compile-time error."

"let alsoIncrementByTen = incrementByTen
alsoIncrementByTen()
// returns a value of 50

incrementByTen()
// returns a value of 60

上面的例子表明调用 alsoIncrementByTen 和调用 incrementByTen 是一样的.因为它们都引用同一个闭包,所以它们都递增并返回相同的运行总数.

The example above shows that calling alsoIncrementByTen is the same as calling incrementByTen. Because both of them refer to the same closure, they both increment and return the same running total.

转义闭包当闭包作为参数传递给函数时,闭包被称为对函数进行转义,但在函数返回后被调用.当您声明一个将闭包作为其参数之一的函数时,您可以在参数类型前写上 @escaping 以指示允许闭包转义.

Escaping Closures A closure is said to escape a function when the closure is passed as an argument to the function, but is called after the function returns. When you declare a function that takes a closure as one of its parameters, you can write @escaping before the parameter’s type to indicate that the closure is allowed to escape.

闭包可以转义的一种方法是存储在函数外部定义的变量中.例如,许多启动异步操作的函数将闭包参数作为完成处理程序.函数在开始操作后返回,但直到操作完成才调用闭包——闭包需要转义,稍后再调用.例如:

One way that a closure can escape is by being stored in a variable that is defined outside the function. As an example, many functions that start an asynchronous operation take a closure argument as a completion handler. The function returns after it starts the operation, but the closure isn’t called until the operation is completed—the closure needs to escape, to be called later. For example:

var completionHandlers: [() -> Void] = []
func someFunctionWithEscapingClosure(completionHandler: @escaping () -> Void) {
    completionHandlers.append(completionHandler)
}

someFunctionWithEscapingClosure(_:) 函数接受一个闭包作为它的参数,并将它添加到一个声明为[…]的数组中"

The someFunctionWithEscapingClosure(_:) function takes a closure as its argument and adds it to an array that’s declared[…]"

摘自:Apple Inc.The Swift Programming Language (Swift 5.2)".苹果书.https://books.apple.com/us/book/the-swift-programming-language-swift-5-2/id881256329

Excerpt From: Apple Inc. "The Swift Programming Language (Swift 5.2)." Apple Books. https://books.apple.com/us/book/the-swift-programming-language-swift-5-2/id881256329

换句话说,如果您尝试引用当前对象中的方法,@escaping 闭包需要使用 self.即 SKActions 的完成关闭就是一个很好的例子.

In other words, @escaping closures require the use of self, if you are trying to reference methods within the current object. i.e. Completion Closures of SKActions are a really good example of this.

并且非转义闭包不需要使用 self.因为它们不用于引用对象.

And non escaping closure's don't require the use of self. Because they weren't used to reference an object.

这篇关于在 Swift 中,(()->()) 和 @escaping() -> 有什么区别?空白?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆