有A类:
@interface ClassA : NSObject {
}
@property (nonatomic, assign) id prop1;
@end
@implementation
@synthesize prop1;
@end
然后我有子类
@interface ClassB : ClassA {
}
@end
@implementation
- (id)init {
self = [super init];
if (self) {
}
return self;
}
//This is infinite loop
- (void) setProp1:(id)aProp
{
self.prop1 = aProp;
}
@end
这是无限循环,因为 ClassB 中的 setProp1 从 ClassB 中调用 [ClassB setProp1:val]。
我已经尝试过调用 [super setProp1] 但是这个
如何覆盖 @property 并在覆盖的 setter 中赋值?假设我不能修改 ClassA。
最佳答案
直接赋值给实例变量,不用点语法调用setter:
- (void) setProp1:(id)aProp
{
self->prop1 = aProp;
}
不过,那是个问题。所有这些访问器所做的正是父级所做的 - 那么重写父级的意义何在?
关于objective-c - 覆盖@property setter 和无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6405397/