Swift 1.2 中的 @noescape 属性 [英] @noescape attribute in Swift 1.2

查看:29
本文介绍了Swift 1.2 中的 @noescape 属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Swift 1.2 中有一个新属性,函数中有闭包参数,正如文档所述:

There is a new attribute in Swift 1.2 with closure parameters in functions, and as the documentation says:

这表明参数只被调用(或作为@noescape 参数),这意味着它不能超过调用的生命周期.

This indicates that the parameter is only ever called (or passed as an @ noescape parameter in a call), which means that it cannot outlive the lifetime of the call.

据我了解,在此之前,我们可以使用 [weak self] 来不让闭包具有强引用,例如它的类,self 可以是 nil 或执行闭包时的实例,但是现在,@noescape 意味着如果类被取消初始化,闭包将永远不会被执行.我理解正确吗?

In my understanding, before that, we could use [weak self] to not let the closure to have a strong reference to e.g. its class, and self could be nil or the instance when the closure is executed, but now, @noescape means that the closure will never be executed if the class is deinitalized. Am I understand it correctly?

如果我是正确的,当它们的行为非常相似时,为什么我要使用常规函数的 @noescape 闭包?

And if I'm correct, why would I use a @noescape closure insted of a regular function, when they behaves very similar?

推荐答案

@noescape 可以这样使用:

func doIt(code: @noescape () -> ()) {
    /* what we CAN */

    // just call it
    code()
    // pass it to another function as another `@noescape` parameter
    doItMore(code)
    // capture it in another `@noescape` closure
    doItMore {
        code()
    }

    /* what we CANNOT do *****

    // pass it as a non-`@noescape` parameter
    dispatch_async(dispatch_get_main_queue(), code)
    // store it
    let _code:() -> () = code
    // capture it in another non-`@noescape` closure
    let __code = { code() }

    */
}

func doItMore(code: @noescape () -> ()) {}

添加 @noescape 保证闭包不会被存储在某个地方、稍后使用或异步使用.

Adding @noescape guarantees that the closure will not be stored somewhere, used at a later time, or used asynchronously.

从调用者的角度来看,不需要关心捕获变量的生命周期,因为它们在被调用函数中使用或根本不使用.作为奖励,我们可以使用隐式 self,从而避免键入 self..

From the caller's point of view, there is no need to care about the lifetime of captured variables, as they are used within the called function or not at all. And as a bonus, we can use an implicit self, saving us from typing self..

func doIt(code: @noescape () -> ()) {
    code()
}

class Bar {
    var i = 0
    func some() {
        doIt {
            println(i)
            //      ^ we don't need `self.` anymore!
        }
    }
}

let bar = Bar()
bar.some() // -> outputs 0

另外,从编译器的角度来看(如 release注释):

Also, from the compiler's point of view (as documented in release notes):

这可以实现一些小的性能优化.

This enables some minor performance optimizations.

这篇关于Swift 1.2 中的 @noescape 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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