Understanding Properties in Objective-C and iOS

I think one of the first questions all new iOS developers ask is “Why declare properties with attributes like (nonatomic, strong)?” This is especially true for those who learn iOS development through the Stanford Paul Hegarty iOS lectures.

Understanding properties in Objective-C and iOS can be tough the first go around. When I first started programming for iOS I didn’t really understand all the ins and outs of properties and I probably didn’t need to because I wasn’t writing complex applications.

Over the last 8 months I’ve done a fair amount of iOS programming and have run into a number of scenarios where I needed to create read-only properties and thread-safe objects/classes. Some times Stackoverflow.com just doesn’t cut it because there are so many different questions and accepted answers it’s hard to really tell when and why to code what. You always run the risk of just coding something that works, but not really understanding if it’s the best way or not. If you find yourself in this situation–wanting to better understand how to create a property with the right set of attributes (i.e. nonatomic vs atomic, etc.)–then the Programming for Objective-C guide for iOS is the right place to start.

I’d recommend against thinking you’re better off taking shortcuts by just digging around on Stackoverflow.com or random blog posts because reading documentation is boring and goes into unnecessary detail, especially if you want to grasp the fundamentals of when and why to code properties with specific attributes. The official documentation excels in this area because it provides the necessary level of detail with helpful examples. I’ve found it to be much more helpful in understanding the fundamentals of properties in iOS development. However, once you have read the documentation, the topics on Stackoverflow.com (and other sites) become much more meaningful and are very worthwhile.

Instead of paraphrasing the already concise documentation I’ll just summarize some of the key topics you can read about.** If you’ve been coding in Obj-C for a little while already I recommend jumping into the “Data Encapsulation” section of the Programming for Objective-C guide. This section discusses the following concepts:

  • Default property attributes (i.e. readwrite, strong, atomic)
  • When/Why to use property attributes with helpful examples
    • readwrite vs readonly
    • strong vs weak
    • atomic vs nonatomic
    • unsafe_unretained
    • copy
  • When/Why to use variable qualifiers
    • __strong vs __weak
    • __unsafe_unretained
  • Specifying getter and setter names that are different from property names (w/o implementing the method code)
  • Strong reference cycles and how to avoid them
  • Why/How you should “cache” weak variables

If you want further reading on variable qualifiers as it pertains to Automatic Reference Counting (ARC) then I recommend reading the Transitioning to ARC Release Notes. This documentation is quite insightful covers more on variable qualifiers in addition to two examples I found very helpful, namely an example of how the compiler interprets an implicit strong pointer to NSError and an example involving blocks and the __block qualifier.

**This begs the question “Why the blog post then if you aren’t feeding me any new information?” My goal is to provide a point of reference so that I and others will know where to look in the future. It’s not always easy to know where to look when there seems to be endless documentation on all possible subject matter. Furthermore, I’ve summarized the most important topics so I and others know whether that source will contain the information sought.