如果您的公共界面中有属性,如下所示
@interface MyClass : NSObject
@property(strong) NSString *myProp;
@end
Run Code Online (Sandbox Code Playgroud)
然后合成它,实际上合成变量:
@implementation MyClass
@synthesize myProp = _myProp; // or just leave it at the default name..
@end
Run Code Online (Sandbox Code Playgroud)
实例变量的可见性是_myProp
多少?也就是说,这是考虑过@public
,@protected
还是@private
?我猜是因为MySubClass
可以从那时继承MyClass
它也会得到属性(自然),但是它还会继承实例变量可见性吗?
如果我将属性放在类扩展中会有什么不同?这会将属性隐藏在子类中,我也猜测实例变量.这记录在哪里?
我通常懒惰地在他们的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;
}
Run Code Online (Sandbox Code Playgroud)
但是当子类化时,我经常想要覆盖一些@properties以使其更具子类.所以我想改变实例化并执行以下操作:
@interface MySpecificSubclass : MyGenericClass
//...
@implementation MySpecificSubclass
- (UIImageView *)infoImageView
{
if (!_infoImageView) {
_infoImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"SpecialInfoImage"]];
}
return _infoImageView;
}
Run Code Online (Sandbox Code Playgroud)
但那是不可能的,因为子类无法访问_infoImageView iVar.
我正在努力做坏事吗?或者有一个共同的解决方案/最佳实践吗?我看到的唯一解决方案是让iVar公开,这感觉违反了封装原则......
感觉这是一个非常基本的问题,那里必须有数百万的答案,但是在搜索了几个小时之后,我发现的一切都是Objective-C:当覆盖超类getter并尝试访问ivar时编译错误 ,但是它没有解决方案.
overriding lazy-loading properties objective-c lazy-initialization
当我@property
在没有声明ivars的情况下声明一个超类中的子类时,将其子类化并尝试使用_propertyName
子类中的超类ivar()实现getter ,xcode调用一个错误说明Use of undeclared identifier '_propertyName'
.
什么是符合最佳编程实践的解决方案?
我应该@synthesize propertyName = _propertyName
在@implementation
子类中还是
@interface SuperClass : AnotherClass
{
Type *_propertyName;
}
@property Type *propertyName;
@end
Run Code Online (Sandbox Code Playgroud)
编辑:
我确实理解属性的访问器方法的自动"综合"和编译器创建的"underbar ivars".
可以通过 接口或实现部分中SuperClass
没有任何@synthesize
ivars声明或声明来实现ivar .
进一步澄清我的案例:免责声明:内容被窃取的Alfie Hanssen代码块
@interface SuperViewController : UIViewController
@property (nonatomic, strong) UITableView * tableView; // ivar _tableView is automatically @synthesized
@end
#import "SuperViewController.h"
@interface SubViewController : SuperViewController
// Empty
@end
@implementation SubViewController
- (void)viewDidLoad
{
NSLog(@"tableView: %@", self.tableView); // …
Run Code Online (Sandbox Code Playgroud)