Reference type inside value type

混江龙づ霸主 提交于 2019-12-01 18:35:24

Every struct A copy will share the same reference to B. Every new struct A, created from scratch, will hold a completely new B object.

The B.deint will be called when there are zero strong references to it (e.g., your var b is one of these strong references). For instance, if only A values hold references to a given B object then those will need to got out of scope to zero all references to this object (or their boxed copies be deallocated as well, but this might be a topic for another question.)

Code Design. If these all sounds too confusing and is blocking your app progress (with no real practical benefit so far), you might consider refactoring B to a struct as well. For instance, even Apple recommends considering value types to design your model layer. This blog post might also help make up your mind.

You can test this in a playground:

class B {
    var data: Int = 0

    deinit {
        print("deallocated!")
    }
}

struct A {
    var b = B()
}

var a1: A? = A()
var a2: A? = A()
var a3: A? = a1

// Do the two instances of struct A share the same instance of class B?
a1?.b === a2?.b // false

// Do copies of instances of struct A share the same instance of class B?
a1?.b === a3?.b // true

// When will deinit be called?
a1 = nil    // Not yet, a3 still holds a strong reference to the shared instance of class B
a3 = nil    // Now! There are no longer any strong references to the shared instance of class B, so it is deallocated.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!