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

查看:13
本文介绍了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 继承的类时,不需要 self-> 指针.请注意,在父 GenericClass 中没有定义名称为 str 的对象.所以,我的问题是,当没有被引用为 self->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天全站免登陆