如何使用 Xcode 调试器打印出属性的内容? [英] How to print out a property's contents using Xcode debugger?
问题描述
我正在编写一个 iOS 应用程序,我需要使用内置 Xcode 调试器的帮助.假设我有一个名为 HomeViewController
的对象,它具有三个属性
I'm writing an iOS app and I need help using the built-in Xcode debugger. Suppose I have an object called HomeViewController
that has three properties
@property (nonatomic) BOOL finished;
@property (nonatomic, strong) NSArray *myArray;
@property (nonatomic, strong) NSString *myName;
@synthesize finished = _finished, myArray = _myArray, myName = _myName;
假设我在这个类中有一个断点.我将如何查看这些属性的内容?我已经尝试过诸如 po myName
、print myName
和 print [self myName]
之类的东西,但我不知道该怎么做.我尝试过使用 LLDB,但我一直收到这个人遇到的相同错误(lldb 无法打印变量值并出现错误:对 'id' 的引用不明确").这个问题的公认答案是,LLDB 坏了,我应该只使用 GDB;但是,我拒绝接受如此基本的东西被破坏了.
Suppose I have a breakpoint in this class. How would I view the contents of these properties? I've tried things such as po myName
, print myName
and print [self myName]
but I can't figure out how to do this. I've tried using LLDB but I keep getting the same error that this person encountered (lldb fails to print variable values with "error: reference to 'id' is ambiguous") . The accepted answer to this question was, LLDB is broken and that I should just use GDB; however, I refuse to accept that something so fundamental is broken.
尽管如此,我也尝试过使用 GDB 和上面类似的命令;但是,我也无法让 GDB 工作.请帮忙
Nevertheless, I've also tried using GDB with similar commands as above; however, I can't get GDB to work either. Help please
推荐答案
放置断点后,运行,程序停在断点处,将光标悬停在你想看到的变量/值上,如下所示:
Once you place a breakpoint, run, and the program stops at the breakpoint, hover your cursor over the variable/value you want to see like this:
您还可以放置一个 NSLog(@"%@", yourLabel.text);
来查看该标签/其他对象类型的内容.
You could also place an NSLog(@"%@", yourLabel.text);
to view the contents of that label/other object type.
另一种选择是在控制台中运行 GDB,如下所示:
One other option is to run GDB in the console like this:
gdb
attach <your process name>
然后使用po
(print-object)命令查看变量的值,如下:
And then use the po
(print-object) command to view the value of a variable like this:
po variableName
查看原始类型的值(int
、float
、long
、double
、char
等),您可以在控制台中运行 GDB 时使用 print
命令,如下所示:
To view the value of primitive types (int
, float
, long
, double
, char
, etc.), you can just use the print
command while running GDB in the console like this:
print yourPrimitiveVariable
希望这会有所帮助!
使用 po
命令,您可以使用属性名称 (self.myProperty
) 或 ivar 名称(可能是 _myProperty
).我在这里演示一下:
With the po
command, you can print out the value of an object using both the property name (self.myProperty
) or the ivar name (possibly _myProperty
). I demonstrate this here:
这篇关于如何使用 Xcode 调试器打印出属性的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!