TIMEX TIMEX - 7 months ago 23
Swift Question

In Swift, do class pointers always point to the parent?

let x = User()?
y = x
z = y


Reference count for "x" should be 2, is that right?

y = nil


Does
z
become nil, or does it continue to be a
User
?

Answer

Reference count for "x" should be 2, is that right?

Yes, you are passing around references, and you did that twice.

Does z become nil, or does it continue to be User?

No, z does not become nil, since z holds a reference to the User. When y is set to nil, the reference count drops to 1, so it stays alive.

Comments