Lazy Properties

Every language has its own idioms and when I have to learn a new language, I always find that looking at sample code and examples is a great way to learn new techniques.

Something that’s very common in the sample code provided by Apple is the use of lazy properties. It’s certainly not an approach that’s unheard of in other object-oriented languages but the method for transferring the technique to Objective-C is not immediately obvious.

Consider the example of a date formatter used by a view controller. It might be used in multiple places so storing it away to avoid the expense of creating the object multiple times is obviously beneficial. On the other hand, it’s possible that dates may be empty so creating during the initialization of the view controller may be a waste. On a mobile platform, every CPU cycle and consumed byte counts, so it’s worth having an efficient way to deal with such objects.

Creating Lazy Properties

In the interface for a class, define a strong property in the usual way.

@interface MyViewController
@property (nonatomic, strong) NSDateFormatter dateFormatter;

Then in the implementation file for the class, a view controller for example, you can override the accessor (getter) method for the property to instantiate it lazily. This is the key to lazy properties: the fact that a synthesized accessor method can be overwritten with your own implementation.

For objects like date formatters that are only used in the main thread for formatting dates for the user interface, there’s no need to worry about thread safety and synchronization but some thought needs to be given to thread safety if you are doing some background work that also uses the property.

Note the best-practice use of the underscore prefix to what becomes the underlying instance variable to avoid confusion between use of the accessor via self.dateFormatter and use of the instance variable directly using _dateFormatter.

@implementation MyViewController

@synthesize dateFormatter = _dateFormatter;

- (NSDateFormatter *)dateFormatter
    if (!_dateFormatter) {
        _dateFormatter = [[NSDateFormatter alloc] init];
        _dateFormatter.dateStyle = NSDateFormatterMediumStyle;
        _dateFormatter.timeStyle = NSDateFormatterShortStyle;
    return _dateFormatter;

// Other methods here ...


The result is a property that will be instantiated the first time it is accessed using self.dateFormatter — but not before and never after.

Benefits of Lazy Properties

Creating lazy properties as shown above has a number of benefits:

  1. Reduces clutter in initialization code and other methods
  2. Encapsulates the creation of the object in a single, easy to find method
  3. Reduces memory footprint by only creating objects on demand
  4. Easy to push the self-contained code into a parent class or utility class

You can learn more about working with dates in Objective-C, including the use and benefits of date formatters for localization, on Learning Tree’s iPhone® and iPad® Programming: A Comprehensive Hands-On Introduction course.

Richard Senior

Type to search

Do you mean "" ?

Sorry, no results were found for your query.

Please check your spelling and try your search again.