在超类中MyClass
:
@interface MyClass : NSObject
@property (nonatomic, strong, readonly) NSString *pString;
@end
@implementation MyClass
@synthesize pString = _pString;
@end
在子类中 MySubclass
@interface MySubclass : MyClass
@end
@implementation MySubclass
- (id)init {
if (self = [super init]) {
_pString = @"Some string";
}
return self;
}
问题在于,编译器认为它_pString
不是的成员MySubclass
,但是在中访问它没有问题MyClass
。
我想念什么?
实例变量_pString
所生产@synthesize
是私人到MyClass
。您需要对其进行保护,以便MySubclass
能够访问它。
_pString
在的@protected
部分添加一个ivar声明MyClass
,如下所示:
@interface MyClass : NSObject {
@protected
NSString *_pString;
}
@property (nonatomic, strong, readonly) NSString *pString;
@end
现在,像往常一样合成访问器,您的变量将可以被您的子类访问。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句