在Objective-C中覆盖延迟加载的属性getter
发布时间:2020-12-16 03:18:17 所属栏目:百科 来源:网络整理
导读:我通常懒惰地在他们的getter方法中实例化我的@property对象,如下所示: @interface MyGenericClass : UIViewController@property(nonatomic,readonly) UIImageView *infoImageView// ...@implementation GenericClass- (UIImageView *)infoImageView{ if (!_
我通常懒惰地在他们的getter方法中实例化我的@property对象,如下所示:
@interface MyGenericClass : UIViewController @property(nonatomic,readonly) UIImageView *infoImageView // ... @implementation GenericClass - (UIImageView *)infoImageView { if (!_infoImageView) { _infoImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"PlaceholderInfoImage"]]; } return _infoImageView; } 但是当子类化时,我经常想要覆盖一些@properties以使其更具子类.所以我想改变实例化并执行以下操作: @interface MySpecificSubclass : MyGenericClass //... @implementation MySpecificSubclass - (UIImageView *)infoImageView { if (!_infoImageView) { _infoImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"SpecialInfoImage"]]; } return _infoImageView; } 但那是不可能的,因为子类无法访问_infoImageView iVar. 我正在努力做坏事吗? 感觉这是一个非常基本的问题,那里必须有数百万的答案,但在搜索了几个小时之后我才发现它是Objective-C: Compiler error when overriding a superclass getter and trying to access ivar 解决方法
您可能希望将_infoImageView声明为头文件中的受保护变量以及属性.
另一个想法是创建一个公共defaultImageView方法来调用惰性getter. 像这样的东西: @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"]]; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |