1.17. Moving From Manual Reference Counting to Automatic Reference Counting
Problem
You want to learn about Automatic Reference Counting, Apple’s new Compiler solution to solving the headache that programmers had to deal with when working with objects and memory management in Objective-C.
Note
Automatic Reference Counting eliminates many of the manual reference counting issues that ultimately resulted in iOS apps that would crash here and there, and would be very unstable when deployed on user devices. ARC removes this headache by leaving most of the memory management complexity to the compiler.
Solution
Study the new storage attributes introduced with the latest
LLVM compiler: strong, weak, and unsafe_unretained.
Discussion
In the latest LLVM complier, to use Automatic Reference Counting (ARC), we will need to deal with storage that is strong, weak, or unsafe and unretained. Any object under ARC is managed with one of these storage attributes. Here is a short explanation for each one:
strongAn object of this type is automatically retained at runtime and will be valid until the end of its scope, where it will automatically be released. For those familiar with Objective-C’s traditional way of memory management, this keyword is similar to the
retainkeyword.weakThis is zeroing weak referencing. If a variable is defined with this keyword, when the object to which this variable points gets deallocated, this value will get set to
nil. For instance, if you have a strong string property and a ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access