弱引用和非引用之间有什么区别? [英] What is the difference between a weak reference and an unowned reference?

查看:163
本文介绍了弱引用和非引用之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

雨燕有

  • 强引用
  • 参考文献不足
  • 无人参考

无主引用与弱引用有何不同?

How is an unowned reference different from a weak reference?

什么时候可以安全使用无主引用?

When is it safe to use an unowned reference?

没有人引用C/C ++中的悬挂指针这样的安全风险吗?

Are unowned references a security risk like dangling pointers in C/C++?

推荐答案

weakunowned引用都不会在所引用的对象上创建strong保留(也就是它们不会按顺序增加保留计数)以防止ARC取消分配所引用的对象).

Both weak and unowned references do not create a strong hold on the referred object (a.k.a. they don't increase the retain count in order to prevent ARC from deallocating the referred object).

但是为什么要使用两个关键字?这种区别与Optional类型是Swift语言内置的事实有关.长话短说:可选类型提供内存安全性(与

But why two keywords? This distinction has to do with the fact that Optional types are built-in the Swift language. Long story short about them: optional types offer memory safety (this works beautifully with Swift's constructor rules - which are strict in order to provide this benefit).

weak引用允许它成为nil的可能性(当释放引用的对象时自动发生),因此属性的类型必须是可选的-因此,作为程序员,您必须在使用它之前先进行检查(基本上,编译器会尽力迫使您编写安全的代码).

A weak reference allows the possibility of it to become nil (this happens automatically when the referenced object is deallocated), therefore the type of your property must be optional - so you, as a programmer, are obligated to check it before you use it (basically the compiler forces you, as much as it can, to write safe code).

一个unowned引用假定它在其生存期内永远不会成为nil.初始化期间必须设置一个无主引用-这意味着该引用将被定义为一种非可选类型,无需检查即可安全使用.如果以某种方式释放了要引用的对象,则当使用无主引用时,应用程序将崩溃.

An unowned reference presumes that it will never become nil during its lifetime. An unowned reference must be set during initialization - this means that the reference will be defined as a non-optional type that can be used safely without checks. If somehow the object being referred to is deallocated, then the app will crash when the unowned reference will be used.

Apple文档 :

只要有效,只要使用弱引用即可 在其生命周期的某个时刻为零.相反,使用无主 引用,当您知道引用将永远不会为零时 已在初始化期间设置.

Use a weak reference whenever it is valid for that reference to become nil at some point during its lifetime. Conversely, use an unowned reference when you know that the reference will never be nil once it has been set during initialization.

在文档中,有一些示例讨论了保留周期以及如何打破它们.所有这些示例均摘录自文档.

In the docs, there are some examples that discuss retain cycles and how to break them. All these examples are extracted from the docs.

weak关键字的示例:

class Person {
    let name: String
    init(name: String) { self.name = name }
    var apartment: Apartment?
}

class Apartment {
    let number: Int
    init(number: Int) { self.number = number }
    weak var tenant: Person?
}

现在,对于某些ASCII艺术(您应该

And now, for some ASCII art (you should go see the docs - they have pretty diagrams):

Person ===(strong)==> Apartment
Person <==(weak)===== Apartment

PersonApartment示例显示了一种情况,其中两个属性都被设置为nil,都可能导致强大的参考周期.最好使用弱引用来解决此情况.这两个实体可以存在,而不必严格依赖彼此.

The Person and Apartment example shows a situation where two properties, both of which are allowed to be nil, have the potential to cause a strong reference cycle. This scenario is best resolved with a weak reference. Both entities can exist without having a strict dependency upon the other.

unowned关键字的示例:

class Customer {
    let name: String
    var card: CreditCard?
    init(name: String) { self.name = name }
}

class CreditCard {
    let number: UInt64
    unowned let customer: Customer
    init(number: UInt64, customer: Customer) { self.number = number; self.customer = customer }
}

在此示例中,Customer可能具有或没有CreditCard,但是CreditCard 将始终Customer关联.为了表示这一点,Customer类具有可选的card属性,但是CreditCard类具有非可选的(且没有所有权)customer属性.

In this example, a Customer may or may not have a CreditCard, but a CreditCard will always be associated with a Customer. To represent this, the Customer class has an optional card property, but the CreditCard class has a non-optional (and unowned) customer property.

Customer ===(strong)==> CreditCard
Customer <==(unowned)== CreditCard

CustomerCreditCard示例显示了一种情况,其中一个允许为nil的属性和另一个不能为nil的属性有可能导致强参考循环.最好使用无主引用解决此情况.

The Customer and CreditCard example shows a situation where one property that is allowed to be nil and another property that cannot be nil has the potential to cause a strong reference cycle. This scenario is best resolved with an unowned reference.

Apple的注意事项:

Note from Apple:

弱引用必须声明为变量,以指示它们 值可以在运行时更改.弱引用不能声明为 常数.

Weak references must be declared as variables, to indicate that their value can change at runtime. A weak reference cannot be declared as a constant.

还有第三种情况,即两个属性都应始终具有值,并且初始化完成后,两个属性都不应为零.

There is also a third scenario when both properties should always have a value, and neither property should ever be nil once initialization is complete.

在使用闭包时,还可以避免经典的保留周期方案.

And there are also the classic retain cycle scenarios to avoid when working with closures.

为此,我建议您访问 Apple文档,或阅读这本书.

For this, I encourage you to visit the Apple docs, or read the book.

这篇关于弱引用和非引用之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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