Last Updated: February 25, 2016
·
1.986K
· alexdenisov

Lazy initialization in Objective-C

Lazy initialization in Objective-C can be simple done via BloodMagic

@­interface ViewController : UIViewController
    <BMLazy>

@­property (nonatomic, strong) ProgressViewService *progressViewService;
@­property (nonatomic, strong) ResourceLoader *resourceLoader;

@­end

@­implementation ViewController

@­dynamic progressViewService;
@­dynamic resourceLoader;

- (void)loadResources
{
    [self.progressViewService showProgressInView:self.view];

    self.resourceLoader.delegate = self;
    [self.resourceLoader loadResources];
}

@­end

And that’s it. Both @dynamic properties are created by a first call to self.progressViewService and self.resourceLoader.