i have little bit confusion in ARC reference count can you please tell me what will be reference count of bellow code.
var vc1 = UIViewController()
var vc2 = vc1
var vc3 = vc2
weak var vc4 = vc3
vc3 refer to the same object. So, the reference count of that object is 3. When
vc4 refer to the same object, since it is weak reference, the reference count will not be incremented by 1. So, the reference count after this will also be 3
The reference count of
UIViewController object that is created and referred by
vc1 after first line of code is 1.
var vc1:UIViewController = UIViewController()
vc2 refers to the same object as
vc1. The reference count of object turns to 2
var vc2:UIViewController = vc1
vc3 refers to the same object as
vc2. The reference count of object turns to 3
var vc3:UIViewController = vc2
vc4 refers to the same object as
vc4 is weak reference, the reference count will not be incremented. That means the count is still 3.
weak var vc4:UIViewController? = vc3
What it means:
Execute the following code.
vc1 = nil; // reference count = 3-1 = 2 vc2 = nil; // reference count = 2-1 = 1 vc3 = nil; // reference count = 1-1 = 0 and object is destroyed
Now, print the value of
vc4. It will be
nil. This happens because the reference count of object turns to zero and all of the variables refers to same object.