Swift 5.8 is the minor release of a programming language that was initially used mostly for native iOS apps. Let’s see what’s new!
Relaxed explicit self rules
Swift is a language that relies on a memory management method called an Automatic Reference Counting (ARC).
It is one of the three main methods of handling memory. Two others are:
managing memory manually
It is a method used in C language, where you allocate and deallocate memory for variables on your own.
automatic reference counting
Automatic Reference Counting is somewhere in the middle between releasing memory manually and garbage collection.
It works by keeping track of the number of references to an object. If the number becomes zero, object is not used, so the memory can be released.
what is the problem with ARC?
If an object A references object B, and object B references object A, a garbage collector will be able to release them (of course if nothing else references them), because they can not be found in the tree of objects.
Unfortunately, ARC won’t be able to release them, because both will have a reference number = 1. Such situation is called a retain cycle or cyclic reference.
solution: weak reference
For a long time Swift offered a way to prevent such situation by indicating a weak reference.
Declaring a variable as weak, does not increase reference count. It means that the variable memory can be released when needed.
Practically it means a weak variable can be a nil, so you can’t use self implicitly.
Usually the problem happens when referencing self from a closure or a function. Meaning you have to type self?. before calling a method often.
the improvement in Swift 5.8