Skip to content

Instantly share code, notes, and snippets.

@soffes
Created January 9, 2014 21:09
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save soffes/8342091 to your computer and use it in GitHub Desktop.
Save soffes/8342091 to your computer and use it in GitHub Desktop.
// I wish something like this (with a better name for the keyword):
@missing UILabel *titleLabel {
UILabel *label = [[UILabel alloc] init];
return label;
};
// Could turns into this:
@interface ViewController ()
@property (nonatomic, readonly) UILabel *titleLabel;
@end
@implementation ViewController
@synthesize titleLabel = _titleLabel;
- (UILabel *)titleLabel {
if (!_titleLabel) {
UILabel *label = [[UILabel alloc] init];
_titleLabel = label;
}
return _titleLabel;
}
@end
@kvnsmth
Copy link

kvnsmth commented Jan 9, 2014

What about?

@property (nonatomic, readonly, autoinitialize) UILabel *titleLabel

@gamenerds
Copy link

@kvnsmth, Nice. I'd even shorten to 'autoinit'.

@calebd
Copy link

calebd commented Jan 10, 2014

This:

#define CMDLazyLoad(ivar, block) \
@synthesize ivar = _##ivar; \
- (id)ivar {\
    if (!_##ivar) {\
        block();\
    }\
    return _##ivar;\
}

let's you write this:

CMDLazyLoad(settingsButton, ^{
    _settingsButton = [UIButton new];
    [_settingsButton setImage:[UIImage imageNamed:@"Settings"] forState:UIControlStateNormal];
    [_settingsButton addTarget:self action:@selector(showSettings) forControlEvents:UIControlEventTouchUpInside];
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment