自动属性合成(@property)和继承

时间:2014-03-13 16:07:02

标签: ios objective-c properties warnings xcode5.1

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

这个想法是拥有一个对象(一个模型),它是从原始类继承的可变版本。因此,我们的想法是打开一个readonlyreadwrite

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

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

那些需要在单独的文件中(比如两个普通的类)。

它给出了这个警告:

Auto property synthesis will not synthesize property 'name' because it is 'readwrite' but it will be synthesized 'readonly' via another property

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

1 个答案:

答案 0 :(得分:59)

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

@implementation MutableCar

@synthesize name;

@end

这样clang不会尝试使用autosynthesis

编辑:

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

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

@interface Car : NSObject

- (void)printVar;

@property (strong, readonly) NSString *name;

@end

现在在.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

现在您可以像以前一样创建MutableCar.h:

@interface MutableCar : Car

@property (strong, readwrite) NSString *name;

@end

你的MutableCar.m应该是这样的:

@implementation MutableCar

@dynamic name;

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

@end

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

相关问题