大佬教程收集整理的这篇文章主要介绍了在Objective-C中覆盖延迟加载的属性getter,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
@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.
我正在努力做坏事吗?
或者有一个共同的解决方案/最佳实践吗?我看到的唯一解决方案是让iVar公开,这感觉违反了封装原则……
感觉这是一个非常基本的问题,那里必须有数百万的答案,但在搜索了几个小时之后我才发现它是Objective-C: Compiler error when overriding a superclass getter and trying to access ivar
,但它没有提供解决方案.
@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"]]; }@H_404_16@ @H_404_16@
以上是大佬教程为你收集整理的在Objective-C中覆盖延迟加载的属性getter全部内容,希望文章能够帮你解决在Objective-C中覆盖延迟加载的属性getter所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。