自动属性合成(@property)和继承 [英] Auto property synthesis (@property) and inheritance

查看:321
本文介绍了自动属性合成(@property)和继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用XCode 5.1,会出现一个新警告。这让我理解 -obviously - 我做错了什么。



想法是有一个对象(一个模型)而且它是可变的继承自原始类的版本。所以想法是打开一个 readonly 读取


$ b $的属性b

  @interface Car:NSObject 
@property(strong,readonly)NSString * name;
@end

@interface MutableCar:Car
@property(strong,readwrite)NSString * name;
@end

这些需要在单独的文件中(例如两个普通类)。 / p>

它给出了这个警告:

 自动属性合成不会合成属性'name',因为它是'readwrite'但它将通过另一个属性'readonly'合成

所以我想知道什么是正确的解决方案来做类似的事情,如果它甚至可能的话。如果它需要编写访问器并避免使用自动合成等。请准确,并用文档或其他任何方式支持你的答案。

解决方案

<我建议在MutableCar实现上明确合成该属性。如:

  @implementation MutableCar 

@synthesize name;

@end

这样clang不会尝试使用自动合成



编辑:



如果您不想使用封装,并且出于其他原因需要从父类访问ivar,那么您需要多做一点努力:



首先,Car .h文件保持不变(我添加了printVar方法来打印ivar和属性):

  @interface Car:NSObject 

- (void)printVar;

@property(strong,readonly)NSString * name;

@end

现在在.m文件中,我正在实施printVar方法并添加一个类扩展来告诉clang创建setter:

  //私有类扩展,导致setName:要创建但不暴露。 
@interface Car()

@property(strong,readwrite)NSString * name;

@end

@implementation Car

- (void)printVar
{
NSLog(@< Car> ; Hello%@,ivar:%@,self.name,_name);
}

@end

现在你可以创建你的MutableCar .h和以前一样:

  @interface MutableCar:Car 

@property(strong,readwrite)NSString *名称;

@end

你的MutableCar.m应如下所示:

  @implementation MutableCar 

@dynamic name;

- (无效)printVar
{
[super printVar];
NSLog(@< MutableCar> Hello%@,self.name);
}

@end

这样_name ivar on父实际上是使用父设置器编写的,您可以访问它。


With XCode 5.1, a new warning appears. It made me understand -obviously- that I was doing something wrong.

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

@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.

解决方案

I'd suggest to explicitly synthesize the property on your MutableCar implementation. As in:

@implementation MutableCar

@synthesize name;

@end

That way clang won't try to use autosynthesis

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:

First the Car .h file remains the same (I added a printVar method to print the ivar and property):

@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:

// 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:

@interface MutableCar : Car

@property (strong, readwrite) NSString *name;

@end

and your MutableCar.m should looks like this:

@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.

这篇关于自动属性合成(@property)和继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆