Get Updates By E-mail 

Article Written By:
Johann Dowa

A Macro For Easy Clean Lazy Instantiation Of Properties In Objective-C

  •  
  •  
  •  
  •  
  •  

At this start of this month I mentioned an interesting project providing an NSString category that makes it easy to get UIColor selectors by name or value from Nicolas Goutaland.

Here’s another interesting project submitted by Nicolas that makes it easy to perform lazy instantiation (initialized only when used) of Objective-C properties saving on boilerplate code called LazyProperty.

Lazyproperty includes a simple macro that will automatically make properties use the lazy instantiation pattern (properties must be set to nonatomic, strong).

This source code snippet from the readme shows the macro in action:

@interface DemoViewController ()
...

// Lazy properties
@property (nonatomic, strong) SimpleViewController *simpleViewController; // Will be used modally
@property (nonatomic, strong) DetailViewController *detailViewController; // Will be pushed from tableview
@end

@implementation DemoViewController
...

// Magic happens here. Write macros at the end of the file, to keep it clean. Use property name
LAZY_PROPERTY(simpleViewController);
LAZY_PROPERTY(detailViewController);
@end

You can find LazyProperty on Github here.

A nice macro for easy lazy instantiation saving on code typing and making for easier memory management.


  •  
  •  
  •  
  •  
  •  

Submit A Resource

Have you created a useful tutorial, library or tool for iOS development that you would like to get in front of our 300,000+ monthly page views from iOS developers?

You can submit the url here.

The resources we feel will appeal to our readers the most will be posted on the front page.

Comments