【问题标题】:How to display complex object in debugger?如何在调试器中显示复杂对象?
【发布时间】:2011-02-20 23:17:55
【问题描述】:

我想显示属性 myarray 的内容,来自以下单例:

[Session sharedManager].myarray

我试过这些:

po [Session sharedManager]. myarray

po [[Session sharedManager] myarray]

但总是得到这个错误:

A syntax error near end of expression.

有什么建议吗?

--- 编辑 ---

我正在使用 SDK 3.0。

我发现了问题,我有三个开放括号,而不是两个。你在这里看不到,因为我打错了括号的数量。它现在正在工作。谢谢。

【问题讨论】:

  • @vodkhang 是print-object的缩写

标签: iphone cocoa-touch xcode debugging


【解决方案1】:

如果您使用 XCode 调试器并在变量已初始化且可以看到的某个位置设置断点,则可以单击该变量并选择“打印说明”。

你可以用像 NSLog() 这样简单的方法来完成它。这种方法有什么问题?通常,我看到它会打印出数组中所有对象的所有 description() 方法?

我不确定,但您在语句末尾缺少分号。 “;”,你能重新检查一下吗?

【讨论】:

  • gdb 表达式不以分号结尾
  • 打印描述使用与 GDB 中的打印对象相同的功能。 (他们都在表达式的结果上调用 -debugDescription。)正如 Brad Smith 所展示的,OP 的方法 (po [[Session sharedManager] myarray]) 应该可以正常工作,没有语法错误。
  • 遇到断点时看不到打印说明。您能详细说明如何访问它吗?
  • 通常您可以通过在调试器视图中右键单击变量来获得它。
  • @Brad Smith:嗯,我明白了,因为第一次,我认为这是正常的代码。 @Quinn Taylor:感谢您的编辑:)
【解决方案2】:

你所描述的很奇怪。我设置了一个测试应用程序,并且能够很好地从单例打印对象。

#import "testAppDelegate.h"

//A Session Singleton
@interface Session : NSObject {
    NSArray *myArray;
}
@property (nonatomic, retain)   NSArray *myArray;
@end

@implementation Session
@synthesize myArray;
static Session *sharedSession;
+(Session *)sharedSession {
    if (!sharedSession) {
        sharedSession = [[Session alloc] init];
        sharedSession.myArray = [NSArray arrayWithObjects:@"A",@"B",@"C",nil];
    }
    return sharedSession;
}
@end


//App Delegate
@implementation testAppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    NSLog(@"%@",@"Breakpoint Here"); //Here is where I set My breakpoint
    return YES;
}


- (void)dealloc {
    [super dealloc];
}

@end

在 GDB 中:

(gdb) po [[Session sharedSession] myArray]
<NSCFArray 0x4710630>(
A,
B,
C
)

我使用 3.2 iPhone SDK,使用默认项目模板,在调试模式下执行此操作,而没有更改任何构建设置。我怀疑您的构建设置可能存在问题。我注意到在 4.0 beta sdks 上调试很不稳定。如果您使用的是 4.0,请记住它仍然是测试版,您的问题可能真的是别人的问题。

【讨论】:

  • 是的——你是对的。它确实有效。我已经更新了我的 OP,因为它有一个错字。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多