Reference type inside value type

前端 未结 2 662
别那么骄傲
别那么骄傲 2021-01-19 07:19

I am exploring Swift value types particularly structs to get a better understanding of it\'s uses in different scenario. I was amazed to see how enum can be used to build Bi

2条回答
  •  悲&欢浪女
    2021-01-19 07:55

    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.

提交回复
热议问题