What is ARC
ARC stands for Automatic Reference Counting. It manages memory. Only if reference counting becomes 0, memory will be deallocated.
When does ARC perform?
When it compiles
ARC vs GC
ARC counts when it compiles, so it’s easy to predict when the instance memory will be deallocated, but that also means there is a chance that instance never deallocated in memory.
GC counts when the program runs, so it reduces performance as we need more resources to count references and in contrast to ARC it’s hard to predict when the instance will be deallocated from memory. However, we have more opportunity to deallocate memory even in complex situation and do not need to worry about such rules.
Weak Strong Unowned
Strong owns object and reference count will increase. Retain when assigned and Released when counting ends.
Weak does not own object but only have address, just like point. Although weak does do refer to some object, it can’t deallocate the object. Thus, there is no retain or therefore release. So, we don’t know when and how this memory will be used or deallocated. But once if the memory released, ARC will change this reference to nil. That means, weak object should be optional.
[weak self] let ARC not to count number of property. So, as it won’t be counted, circular reference won’t occur.
Very similar to Weak but unowned cannot be nil. That is we need to use unowned when we certainly know the object will not access to released memory space.
When we know life cycle and we know that we can manage it, we use it instead of weak optional for more short and clean code.
Class vs Struct
Class - Reference Type
it will be saved in heap memory area and managed by ARC
We can share the object itself as reference will be copied
We can inherit
Struct - Value Type
We cannot share this as the value itself will be copied
Safer for multi threading