Sobes.tech
Junior — Middle

When exactly does the reference count of an object increase or decrease?

sobes.tech AI

Answer from AI

The reference count for an object in iOS (ARC — Automatic Reference Counting) increases when a new strong reference to the object is created, and decreases when such a reference is removed or set to nil.

For example:

  • When assigning an object to a variable with a strong reference, the count increases.
  • When the variable goes out of scope or is assigned nil, the count decreases.

When the reference count reaches zero, the object is automatically deallocated from memory.

Example:

var obj: MyClass? = MyClass() // count = 1
var anotherRef = obj          // count = 2
obj = nil                    // count = 1
anotherRef = nil             // count = 0, object is deallocated
When exactly does the reference count of an object… - sobes.tech