Overriding property getters with lazy loading in Objective-C

為{幸葍}努か 提交于 2019-12-10 04:03:05

问题


I usually lazy instantiate my @property objects in their getter methods like this:

@interface MyGenericClass : UIViewController
@property(nonatomic, readonly) UIImageView *infoImageView
// ...

@implementation GenericClass

- (UIImageView *)infoImageView
{
    if (!_infoImageView) {
        _infoImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"PlaceholderInfoImage"]];
    }
    return _infoImageView;
}

But when subclassing, I would often like to override some of the @properties to be more subclass specific. So I'd like to change the instantiation and do something like:

@interface MySpecificSubclass : MyGenericClass
//...

@implementation MySpecificSubclass

- (UIImageView *)infoImageView
{
    if (!_infoImageView) {
        _infoImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"SpecialInfoImage"]];
    }
    return _infoImageView;
}

But that's not possible, because the subclass can't access the _infoImageView iVar.

Is what I'm trying to do bad style? Or is there a common solution / best practice for this? The only solution I see is to make the iVar public, which feels like violating encapsulation principles...

It feels like this is such a very basic question, that there must be millions of answers already out there, but after searching for hours all I could find was Objective-C: Compiler error when overriding a superclass getter and trying to access ivar , but it provides no solution.


回答1:


You might want to declare _infoImageView as a protected variable in the header file alongside with the property. Another idea is to create a public defaultImageView method to call inside the lazy getter. Something like this:

@interface MyGenericClass : UIViewController
@property (nonatomic, readonly) UIImageView *infoImageView

...

@implementation GenericClass

- (UIImageView *)infoImageView
{
    if (!_infoImageView) {
        _infoImageView = [self defaultImageView];
    }
    return _infoImageView;
}

- (UIImageView *)defaultImageView
{
    return [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"PlaceholderInfoImage"]];
}

...

@interface MySpecificSubclass : MyGenericClass

...

@implementation MySpecificSubclass

- (UIImageView *)defaultImageView
{
    return [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"SpecialInfoImage"]];
}



回答2:


You could use the technique that UIViewController uses for its view:

- (UIView *)view{
    if(!_view){
        [self loadView];
        NSAssert(_view, @"View must be set at end of loadView");
    }
    return _view;
}

- (void)loadView{
    // subclasses must set self.view in their override of this.
    NSAssert(NO, @"To be overridden by subclass");
}


来源:https://stackoverflow.com/questions/19276229/overriding-property-getters-with-lazy-loading-in-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!