从父级 class 覆盖 属性 setter
Overriding property setter from parent class
我好像找不到这个问题的答案。
在基础class中我定义了这个
@property (nonatomic, assign) NSInteger foo;
并且有一个习惯setter
- (void)setFoo:(NSInteger)foo {
_foo = foo;
// Do some stuff...
[self sayHello];
}
到目前为止一切顺利!现在我有一个派生的 class,并尝试覆盖 属性 setter
- (void)setFoo:(NSInteger)foo {
_foo = foo + 1;
// Do some different stuff...
// but avoid calling [self sayHello];
}
编译器在派生的 class' 实现上说 Use of undeclared identifier _foo
。
执行此操作的正确方法是什么?
实例变量 _foo
对您的基础 class 的实现是私有的,因此它不能在子classes 中访问。
但是有一些解决方法:
您可以将 foo 存储为受保护的实例变量,然后直接从 subclasses:
访问它
@interface BaseClass : NSObject
{
@protected NSInteger _foo;
}
@property (nonatomic, assign) NSInteger foo;
@end
我好像找不到这个问题的答案。
在基础class中我定义了这个
@property (nonatomic, assign) NSInteger foo;
并且有一个习惯setter
- (void)setFoo:(NSInteger)foo {
_foo = foo;
// Do some stuff...
[self sayHello];
}
到目前为止一切顺利!现在我有一个派生的 class,并尝试覆盖 属性 setter
- (void)setFoo:(NSInteger)foo {
_foo = foo + 1;
// Do some different stuff...
// but avoid calling [self sayHello];
}
编译器在派生的 class' 实现上说 Use of undeclared identifier _foo
。
执行此操作的正确方法是什么?
实例变量 _foo
对您的基础 class 的实现是私有的,因此它不能在子classes 中访问。
但是有一些解决方法:
您可以将 foo 存储为受保护的实例变量,然后直接从 subclasses:
@interface BaseClass : NSObject
{
@protected NSInteger _foo;
}
@property (nonatomic, assign) NSInteger foo;
@end