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.

FacebookTwitterDiggStumbleUponGoogle Plus

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

©2014 iOS App Dev Libraries, Controls, Tutorials, Examples and Tools. All Rights Reserved.

Leave a Reply

Your email address will not be published. Required fields are marked *