简体   繁体   English

什么意思[无主自我],有什么好处?

[英]What means [unowned self] and what are the benefits?

I´m trying to integrate Face/Touch ID Login but i saw in apples documentation [unowned self] in a closure. 我正在尝试集成Face / Touch ID登录,但我在一个闭包中看到了苹果文档[unowned self] What is that and what are the benefits? 那是什么,有什么好处? Example code: 示例代码:

let context = LAContext()
    var error: NSError?

    if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) {
        let reason = "Identify yourself!"

        context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: reason) {
            [unowned self] (success, authenticationError) in

            DispatchQueue.main.async {
                if success {
                    print("Authenticated!")
                } else {
                    // error
                }
            }
        }
    } else {
        // no biometry
    }

Long story short, it's the same as weak! 长话短说,它和weak! , since unowned references are just weak references that are guaranteed to have a value. 因为无主引用只是保证具有值的弱引用。

unowned is used when you're sure that the reference will NEVER be nil, and therefore, it can only be evaluated with non nil values. unowned ,当你确定了参考永远不会为零时使用,因此,它只能与非零值评估。

Like weak references, an unowned reference does not keep a strong hold on the instance it refers to. 与弱引用一样,无主引用并不会强烈保留它所引用的实例。 Unlike a weak reference, however, an unowned reference is assumed to always have a value. 然而,与弱引用不同,假定无主引用始终具有值。 Because of this, an unowned reference is always defined as a non-optional type. 因此,无主引用始终定义为非可选类型。 (Apple Docs) (Apple Docs)

Check this other answer: What is the difference between a weak reference and an unowned reference? 检查另一个答案: 弱引用和无引用引用之间有什么区别?

Docs: ARC Documentation 文档: ARC文档

The unowned qualifier, like weak , prevents the closure from establishing a strong reference to to self which can be helpful in preventing strong reference cycles. unowned限定符就像weak一样,阻止了封闭对self的强烈引用,这有助于防止强引用周期。 The benefit of unowned over weak is that it's a tad more efficient in optimized builds, not requiring it to keep track of this reference and go back and set it to nil when the object to which it references is deallocated. unownedweak的好处是它在优化的构建中更有效,不需要它跟踪这个引用,并且当它引用的对象被释放时返回并将其设置为nil The unowned reference also is not an optional, meaning that you don't have to unwrap it, eliminating syntactic noise and simplifying one's code. unowned参考也不是可选的,这意味着您不必打开它,消除语法噪音并简化一个人的代码。

But you obviously can't use unowned in any situation where the object might be deallocated, because it obviously can no longer keep a reference to the memory for a deallocated object. 但是你很明显不能在任何可能被释放对象的情况下使用unowned ,因为它显然不能再为已释放的对象保留对内存的引用。

Interestingly, the evaluatePolicy(_:localizedReason:reply:) documentation says, “This method asynchronously evaluates an authentication policy.” Any time you're dealing with an asynchronous method, it is not advisable to use unowned , because you cannot be assured that the object in question hasn't been deallocated in the intervening time. 有趣的是, evaluatePolicy(_:localizedReason:reply:) 文档说,“此方法异步评估身份验证策略。”无论何时处理异步方法,都不建议使用unowned ,因为您不能放心有问题的对象在此期间尚未解除分配。 Only use unowned in those specific situations where you know, for a fact, that the closure cannot be called if the object has been deallocated. 只有在您知道事实上,如果对象已被释放,则无法调用闭包的情况下,才使用unowned That would not appear to be the case here. 这似乎不是这种情况。

Bottom line, use unowned to avoid strong reference cycles and where you want cleaner, slightly more efficient code. 最后一行,使用unowned来避免强大的引用周期以及你想要更干净,更高效的代码。 But only do so when you know that it is impossible for the object to be deallocated before closure is called. 只有当你知道在调用闭包之前对象不可能被释放时才这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM