Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
At the Worldwide Developers Conference in 2011, Apple introduced an extension to Objective-C called Automatic Reference Counting, or ARC. Like many object-oriented languages, Objective-C uses the concept of object ownership to keep track of how many places an object is used so that it may know when to retain and release memory associated with objects. This was traditionally handled manually by the programmer using retain and release method calls on the globally-inherited NSObject class. It was messy, but it worked.
The new Automatic Reference Counting feature, or ARC, automates this process. ARC is a pre-compilation phase that automatically creates all of your retain and release calls, so you no longer have to do that manually. This is a fantastic addition to Objective-C, and it makes your job as a programmer a lot easier. Unfortunately, it also creates some new problems, one of which is how to interface with libraries written in plain old C. Let's take a look at an example. Here we have BWDB.h, which is a wrapper, around a C library for the SQLite 3 data base.
And originally I had written this wrapper before ARC was introduced, and it worked just fine, and when ARC was introduced, it gave me trouble around this bit of code right here, which is the Fast Enumeration interface. And the reason for this is that we're passing it plain old C pointers like this stack buffer and ARC was having trouble dealing with that. Now, SQLite is of course, widely used for persistent storage throughout iOS and Fast Enumeration is a feature that's not actually very commonly used but is very, very handy.
SQLite is of course, written in plain old C, and it has no concept of retain and release. It just uses plain old malloc and free for its memory allocation. For the most part, this is not an issue, and it's completely transparent, but when you need the interface with the foundation protocol like Fast Enumeration, it becomes an issue. So, if we look over here in the .h file, we see this NSDictionary enumRows, and this is used right there, and right here in the Fast Enumeration countByEnumeratingWithState method.
So, what I had to do is in order to make this work is I had to declare this as unsafe, un-retained storage. And this is just one of the four storage classes that are available. It's actually a new one that was introduced with ARC and basically it just tells the compiler, when you're doing your ARC stuff, ignore this completely. And that works fine because it's statically allocated, and it doesn't need to be retained and released anyway. The other thing that I had to do is I had to qualify this id for stackbuf with unsafe_unretained.
And again, this was not in the example for the Fast Enumeration on how to implement it in your own class as it's not very well-documented. It's only documented by example and so I kind of had to figure this out myself. But in a nutshell unsafe_unretained tells the system to bypass reference counting and not to zero out pointers. And this makes this particular feature work with ARC. Now, using Fast Enumeration is a convenience, it's not really a requirement. I find it very convenient and powerful and useful.
It's also powerful and useful to understand how ARC works, not just to use it but to understand what it does and how to use it especially in exceptional use cases like this one. So, if you'd like to know more about ARC, this page on the llvm website explains all the gruesome details about how Automatic Reference Counting is implemented in Objective-C. It's a bit dense, but I find that it was worth the effort to read it.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 99339 Viewers
56 Video lessons · 112598 Viewers
71 Video lessons · 81418 Viewers
131 Video lessons · 39086 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.