在斯威夫特,无主与弱参考 [英] In Swift, unowned vs. weak reference

查看:140
本文介绍了在斯威夫特,无主与弱参考的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您的 Dog 具有对 Bone 的弱引用,则意味着Dog在此情况下是引用的所有者,并且它使用骨骼,但骨骼可能会不存在,而狗仍可以起作用(因为对骨骼的引用是可选的)。

If you have a Dog with a weak reference to Bone, that means that the Dog is the 'owner' of the reference in this situation, and it USES bone, but bone can go out of existence and Dog can still function (because the reference to bone is an optional).

然而,对于无主,似乎关键字无主不是在所有者的引用声明中使用,而是在另一个对象中使用。例如,Bone对其狗的引用被标记为无主。

However with 'unowned', it seems to be that the keyword 'unowned' is used not in the owner's declaration of the reference, but in the other object. For instance, Bone's reference to its dog is marked as 'unowned'.

无主是不安全的。如果所有者在程序中的某个时刻不存在,它可能会崩溃,并且它不能是可选的。为什么一个人会使用无主而不是弱的参考?

Unowned is not safe. It can crash if the owner goes out of existence at some point in the program, and it cannot be an optional. Why would one ever use unowned as opposed to a weak reference?

为什么不使用弱?从我的理解来看,它只是与大声失败和默默失败有关。在无主的情况下,如果骨头没有狗,应用程序将始终崩溃,而如果我们使用弱,您将最终得到一个仍然存在的骨骼,带有幽灵狗。

Why not just use weak? from my understanding it just has to do with failing loudly vs. failing silently. In the case of an unowned, the app will always crash if bone ends up without a dog, whereas if we use weak, you will end up with a bone that still exists, with a 'ghost' dog.

推荐答案

强&弱引用



Strong & Weak references


弱引用是一个引用,它不会强制保持它引用的实例,因此不会停止ARC处置引用的实例。

A weak reference is a reference that does not keep a strong hold on the instance it refers to, and so does not stop ARC from disposing of the referenced instance.

因此,当A对B的弱引用时,则A NOT 所有者。

So when A has a weak reference to B, then A is NOT the owner.

示例(其中A是 Bone ,B是

Example (where A is Bone and B is Dog)

class Dog {
    var bone: Bone?
}

class Bone {
    weak var belongsTo: Dog?
}

强参考

这里一个 可以拥有 Bone 。在那种情况下,它是该骨头的所有者。所以属性是一个强大的参考。

Here a Dog can have a Bone. In that case it's the owner of that Bone. So the bone property is a strong reference.

弱参考
Bone 可以属于Dog。但是我们需要声明 belongsTo 属性为弱,否则我们有一个强保留周期(这意味着ARC一旦完成它们就不会释放这些对象)。

Weak reference The Bone can belong to a Dog. But we need to declare the belongsTo property as weak, otherwise we have a strong retain cycle (which means ARC is not going to release these object once we are done with them).


重要提示:在这种情况下, Dog 可以不存在 a Bone 。一个 Bone 可以存在没有一个

Important: In this scenario a Dog can exists without a Bone. And a Bone can exists without a Dog.



无主参考



让我们看看另一个例子

Unowned reference

Let's look at another example

class Person {
    var creditCard: CreditCard?
}

class CreditCard {
    unowned var owner: Person

    init(owner: Person) {
        self.owner = owner
    }
}

再次,Person可以拥有一张CreditCard,所以它有一个所有者属性是对 CreditCard 的强烈引用。

Again, Person can own a CreditCard, so it has a the owner property which is a strong reference to CreditCard.


然而 CreditCard CAN NOT 没有人存在。对?
所以在 CreditCard 里面我们想要一个总是会被填充但我们也希望它变弱的房产。

However a CreditCard CANNOT exist without a person. Right? So inside CreditCard we want a property which will always be populated but we also want it to be weak.

这样的东西

weak var owner: Person 
error: 'weak' variable should have optional type 'Person?'

然而,弱物业必须声明为可选所以我们使用无主,这意味着:

However a weak property must be declared as Optional so we use the unowned which means:


我想要一个弱引用,它总是被填充。

I want a weak reference and it will always be populated.

这篇关于在斯威夫特,无主与弱参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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