Skip to content

Instantly share code, notes, and snippets.

@vinnybad
Last active March 15, 2016 22:18
Show Gist options
  • Save vinnybad/746ed1cce9874ef534a3 to your computer and use it in GitHub Desktop.
Save vinnybad/746ed1cce9874ef534a3 to your computer and use it in GitHub Desktop.
/**
* Lazily initializes an object. Mainly used for reducing boilerplate code.
*/
#define DEFINE_LAZY_GETTER(type, name, initialValueBlock) \
- (type)name { \
if (! _ ## name) { \
_ ## name = initialValueBlock(); \
} \
return _ ## name; \
}
// example usage
DEFINE_LAZY_GETTER( Person *, homeOwner, ^{
return [Person new];
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment