【发布时间】:2010-12-06 16:04:06
【问题描述】:
我有一个超类和一个子类,它们都定义了实例变量。
超类的大致轮廓:
/* GenericClass.h */
@interface GenericClass : NSObject {
/* some variables */
}
@end
/* GenericClass.m */
@implementation GenericClass
/* ... */
@end
子类概要:
/* SpecificClass.h */
#import "GenericClass.h"
@interface SpecificClass : GenericClass {
NSMutableString *str;
}
/* SpecificClass.m */
#import "SpecificClass.h"
@implementation SpecificClass
- (void)aMethod {
//Debugger reports str as out of scope
str = [[NSMutableString alloc] initWithCapacity:100];
//Works fine:
self->str = [[NSMutableString alloc] initWithCapacity:100];
//Doesn't compile as I haven't defined @property/@synthesize:
self.str = [[NSMutableString alloc] initWithCapacity:100];
}
当我使用直接从 NSObject 继承的类时,不需要 self-> 指针。请注意,在父 GenericClass 中没有定义名称为 str 的对象。 所以,我的问题是,为什么 str 在不被引用为 self->str 时超出范围?代码本身有效,但我无法使用调试器读取变量
【问题讨论】:
标签: objective-c cocoa inheritance pointers scope