Hands-On Design Patterns with Swift
上QQ阅读APP看书,第一时间看更新

A brief history of reference counting

Before we get into the details of ARC, we need to rewind the time machine to a previous epoch. The year is 1988, and Objective-C is licensed by NeXT and starting to make its way into their operating system. In 1996, Apple acquires NeXT, alongside the release of OS X, a new and modern toolchain based on Objective-C. Objective-C is a strict superset of C, which means that any valid C code is valid Objective-C code, but unlike C, Objective-C has a modern and efficient memory management engine, based on reference counting.

Reference counting is a technique that accounts for the exact number of references to a particular object that exist at any given time in the memory. Once there are no references, an object, it is deallocated. This is a very different memory management technique, as compared to garbage collection, which you can find in Java or JavaScript.

Many benefits come with the use of reference counting. As there is no garbage collector, there is no need to freeze the process when the memory is cleaned up. It also provides benefits in performance and power management.

Let's go back to the pre-ARC era and write some Objective-C. Even if you're an avid Swift developer, understanding the origins of reference counting will help you to write more correct and efficient code.

Don't be afraid of Objective-C, even if the language is somehow verbose, with a lot of brackets; this is a very fun and performant language to work with.