Skip to content

Instantly share code, notes, and snippets.

@luisobo
Created January 9, 2014 21:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luisobo/8342304 to your computer and use it in GitHub Desktop.
Save luisobo/8342304 to your computer and use it in GitHub Desktop.
Lazy property

Usage

@interface Foo : NSObject

@property (nonatomic, strong) NSString *foo;

@end

@implementation Foo

LAZY(foo, ^{
    return @"foo";
});

@end
#define VARNAME(_name) _ ## _name
#define LAZY(_name, _init...) -(id)_name {\
if (!VARNAME(_name)) {\
VARNAME(_name) = _init();\
}\
return VARNAME(_name);\
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment