【问题标题】:Escaping and non-escaping closures in Swift 3? [duplicate]Swift 3 中的转义和非转义闭包? [复制]
【发布时间】:2017-09-14 07:46:42
【问题描述】:

什么是转义闭包? Swift 3 中转义闭包和非转义闭包有什么区别?

【问题讨论】:

标签: swift


【解决方案1】:

转义闭包

当闭包被传递为 函数的参数,但在函数返回后调用。 当您声明一个将闭包作为其之一的函数时 参数,可以在参数类型前写@escaping 表示允许闭包逃逸。

[...]

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

请参阅 Apple Doc 以获得更好的理解:https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Closures.html

【讨论】:

    猜你喜欢
    • 2020-05-04
    • 2017-03-08
    • 2017-01-23
    • 1970-01-01
    • 2017-07-02
    • 1970-01-01
    • 2021-12-20
    • 2018-05-05
    • 1970-01-01
    相关资源
    最近更新 更多