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
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.