5
我有一个超类和一个子类,它们都定义了实例变量。Objective-C:实例变量超出了调试器的范围
超的粗线条:
/* 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的继承类,一个不需要自我>指针。请注意,没有在父GenericClass中定义名称str的对象。 所以,我的问题是,为什么当没有被引用为self> str时,str超出了范围?代码本身工作,但我无法用调试器读取变量
这是GDB工作原理的一个很好的说明,谢谢!但有一件事仍令我困惑。我有超类定义的变量;让我们调用一个NSMutableString * superstr。在SpecificClass的实现中对“superstr”的引用可以正常工作。但是尝试访问SpecificClass.h中定义的变量“str”,在调试器中不使用self>。 GDB如何找到一个继承的变量,但没有在这个类自己的头文件中定义的变量?如果我不使用继承,这也可以工作。看起来像是XCode中的一个错误 – 2009-10-14 17:10:15
在Debugger Console窗口(⇧⌘R)中尝试'po str'和'po superstr'。如果这些结果相同,这个问题与Xcode无关(很可能)。 – 2009-10-14 19:48:37
奇怪的是,po str和po superstr在控制台中工作正常,po自我> str和po自我> superstr。只有在可视化调试UI中,当使用鼠标移动时,没有自我报告的str变量超出范围,但代码中的读/写操作工作得很好。我会将它记录为Apple的错误。同时,我在所有地方都使用自我(尽管我不想),以便可以进行可视化调试。 – 2009-10-15 12:53:23