简体   繁体   English

Swift:无主引用

[英]Swift: Unowned references

The swift documentation says swift 文档说

unowned references are defined using non-optional types.无主引用是使用非可选类型定义的。

But we can define an unowned variable as optional.但是我们可以将一个无主变量定义为可选的。 It doesn't seem like a hard requirement for an unowned to be non-optional.对于 unowned 而言,非可选似乎并不是一个硬性要求。

What is difference between weak optional & unowned optional..?弱可选和无主可选有什么区别..?

 class Person {

        let name: String
        var apartment: Apartment?

        init(name: String) {
            self.name = name
            print("\(name) is being initialized")
        }
        deinit {
            print("\(name) is being deinitialized")
        }
    }


    class Apartment {
        let unit: String
        init(unit: String) { self.unit = unit }
        unowned var tenant: Person?
        deinit { print("Apartment \(unit) is being deinitialized") }
    }

Neither weak nor unowned increase the reference count of an object; weakunowned都不会增加 object 的引用计数; That is they will not prevent an object from being released if there are no remaining strong references.也就是说,如果没有剩余的强引用,它们不会阻止 object 的发布。

The main difference between weak and unowned is what happens when the referred-to object is released; weak和无主之间的主要区别是当引用的unowned发布时会发生什么; a weak reference becomes nil while an unowned reference still holds a (now invalid) reference to the object, so your program will crash if you try and access it. weak引用变为nil ,而无主引用仍然持有(现在无效)对unowned的引用,因此如果您尝试访问它,您的程序将会崩溃。

Using an optional for an weak reference is required, since it can become nil .需要使用可选的弱引用,因为它可以变为nil Using an optional for an unowned reference is syntactically possible, but the semantics aren't sensible.对无主引用使用可选项在语法上是可能的,但语义不合理。 From the Swift Programming language book:来自 Swift 编程语言书:

...an unowned reference is used when the other instance has the same lifetime or a longer lifetime ...当另一个实例具有相同的生命周期或更长的生命周期时,使用无主引用

Given this definition, there is no point at which the unowned property should be nil .鉴于此定义, unowned属性不应该为nil You can see this from your example;您可以从您的示例中看到这一点; Clearly it is possible for an apartment to be without a tenant and a person who is currently a tenant can cease to exist;显然,公寓可能没有租客,而目前是租客的人可能不复存在; If you use unowned with an optional tenant you will be left with an invalid tenant reference.如果您将unowned与可选tenant一起使用,您将得到一个无效的租户参考。

Consider the difference between this and the credit-card example given in the book:考虑一下这与书中给出的信用卡示例之间的区别:

The relationship between Customer and CreditCard is slightly different from the relationship between Apartment and Person seen in the weak reference example above. CustomerCreditCard之间的关系与上面弱引用示例中看到的 Apartment 和 Person 之间的关系略有不同。 In this data model, a customer may or may not have a credit card, but a credit card will always be associated with a customer.在此数据 model 中,客户可能有也可能没有信用卡,但信用卡始终与客户相关联。 A CreditCard instance never outlives the Customer that it refers to. CreditCard实例永远不会超过它所引用的Customer To represent this, the Customer class has an optional card property, but the CreditCard class has an unowned (and non-optional) customer property.为了表示这一点, Customer class 具有可选的卡属性,但信用卡CreditCard具有无主(且非可选)客户属性。

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

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