Created
January 9, 2014 21:09
-
-
Save soffes/8342091 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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, Nice. I'd even shorten to 'autoinit'.
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
What about?