Objective-C:实例变量超出调试器的范围 [英] Objective-C: instance variables out of scope in debugger

查看:120
本文介绍了Objective-C:实例变量超出调试器的范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个超类和一个子类,它们都定义了实例变量。

I have a superclass and a subclass, both of which define instance variables.

超类的粗略轮廓:

/* 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的对象。
所以,我的问题是,为什么str超出范围,当没有引用自 - > str?该代码本身可以工作,但是我不能用调试器读取变量

When I am using classes that inherit directly from NSObject, one doesn't need the self-> pointer. Note that there is no object with the name str defined in the parent GenericClass. So, my question is, why is str out of scope when not referenced as self->str? The code in itself works, but I can't read the variable with the debugger

推荐答案

GDB不是Objective-C编译器。编译器知道像Objective-C方法中的词法作用域,但GDB不知道。

GDB is not an Objective-C compiler. The compiler knows about things like lexical scope within Objective-C methods, but GDB does not. It does, however, understand local variables.

在Objective-C中,每个方法都有一个隐式的 self 参数传递当它被调用时。所以当你看一下 self-> str 时,GDB会解释为像解释任何其他局部变量计算。

In Objective-C, every method has an implicit self parameter passed to it when it's called. So when you look at self->str, GDB is interpreting that like it would interpret any other local variable evaluation.

当你尝试自己评估 str 时,GDB会寻找一个名为 str 的局部变量,没有找到一个,报告它不在范围内。这不是一个错误;这是预期的行为。

When you try to evaluate str on its own, GDB will look for a local variable called str and, not finding one, reports that it's not in scope. This is not an error; this is the expected behavior.

这篇关于Objective-C:实例变量超出调试器的范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆