To elaborate on my comment. Sometimes this technique is good if you have an object that only needs to be configured once and has some configuration involved that you don't want to clutter your init method.
- (UIView *)myRoundedView;
{
if (!_myRoundedView) {
_myRoundedView = [[UIView alloc] initWithFrame:<#some frame#>];
_myRoundedView.layer.cornerRadius = 10.f;
_myRoundedView.backgroundColor = [UIColor colorWithWhite:0.f alpha:0.6f];
_myRoundedView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
}
return _myRoundedView;
}
It's a pretty contrived example but you can start to see the merit. Methods should be like classes and do one thing well. This method happens to return the roundedView I want. If I slapped this code into the init method then the init method would now have to know the nitty gritty details of how to instantiate and configure this view and any other objects that I slap in there.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…