ios 自动属性合成(@property)和继承
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22384364/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Auto property synthesis (@property) and inheritance
提问by AncAinu
With XCode 5.1, a new warning appears. It made me understand -obviously-that I was doing something wrong.
在 XCode 5.1 中,会出现一个新警告。这让我明白——显然——我做错了什么。
The idea was to have an object (a model) and it's mutable version which inherits from the original class. So the idea is to open a property which was readonly
to readwrite
这个想法是有一个对象(一个模型),它是从原始类继承的可变版本。这样的想法是要打开的是一个性质readonly
,以readwrite
@interface Car : NSObject
@property (strong, readonly) NSString *name;
@end
@interface MutableCar : Car
@property (strong, readwrite) NSString *name;
@end
Those needs to be in separate files (like two normal classes).
这些需要在单独的文件中(如两个普通类)。
And it gives this warning :
它给出了这个警告:
Auto property synthesis will not synthesize property 'name' because it is 'readwrite' but it will be synthesized 'readonly' via another property
So I would like to know what is the right solution to do something like it, if it's even possible. if it's needed to write accessors and avoid using auto synthesis, etc. Just please be precise and support your answer with documentation or whatever.
所以我想知道做类似事情的正确解决方案是什么,如果可能的话。如果需要编写访问器并避免使用自动合成等。请准确并使用文档或其他任何内容支持您的答案。
回答by fz.
I'd suggest to explicitly synthesize the property on your MutableCar implementation. As in:
我建议在您的 MutableCar 实现上显式合成属性。如:
@implementation MutableCar
@synthesize name;
@end
That way clang won't try to use autosynthesis
这样 clang 就不会尝试使用自动合成
Edit:
编辑:
If you don't want to use encapsulation and for other reason you need to access the ivar from the parent class, then you need to do a little bit more effort:
如果您不想使用封装并且出于其他原因需要从父类访问 ivar,那么您需要做更多的努力:
First the Car .h file remains the same (I added a printVar method to print the ivar and property):
首先 Car .h 文件保持不变(我添加了一个 printVar 方法来打印 ivar 和属性):
@interface Car : NSObject
- (void)printVar;
@property (strong, readonly) NSString *name;
@end
Now on the .m file, I'm implementing the printVar method and also adding a class extension to tell clang to create the setter:
现在在 .m 文件中,我正在实现 printVar 方法并添加一个类扩展来告诉 clang 创建 setter:
// Private class extension, causes setName: to be created but not exposed.
@interface Car ()
@property (strong, readwrite) NSString *name;
@end
@implementation Car
- (void)printVar
{
NSLog(@"<Car> Hello %@, ivar: %@", self.name, _name);
}
@end
Now you can create your MutableCar.h as before:
现在你可以像以前一样创建你的 MutableCar.h:
@interface MutableCar : Car
@property (strong, readwrite) NSString *name;
@end
and your MutableCar.m should looks like this:
你的 MutableCar.m 应该是这样的:
@implementation MutableCar
@dynamic name;
- (void)printVar
{
[super printVar];
NSLog(@"<MutableCar> Hello %@", self.name);
}
@end
That way the _name ivar on the parent is actually written using the parent setter and you can access it.
这样父级上的 _name ivar 实际上是使用父级 setter 编写的,您可以访问它。