如何使用 Xcode 调试器打印出属性的内容?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11513283/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:54:57  来源:igfitidea点击:

How to print out a property's contents using Xcode debugger?

iosxcodegdblldb

提问by Nosrettap

I'm writing an iOS app and I need help using the built-in Xcode debugger. Suppose I have an object called HomeViewControllerthat has three properties

我正在编写一个 iOS 应用程序,我需要使用内置 Xcode 调试器的帮助。假设我有一个名为的对象HomeViewController,它具有三个属性

@property (nonatomic) BOOL finished;
@property (nonatomic, strong) NSArray *myArray;
@property (nonatomic, strong) NSString *myName;

@synthesize finished = _finished, myArray = _myArray, myName = _myName;

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 myNameand 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.

假设我在这个类中有一个断点。我将如何查看这些属性的内容?我试过的东西,如po myNameprint myNameprint [self myName],但我无法弄清楚如何做到这一点。我试过使用 LLDB,但我不断收到此人遇到的相同错误(lldb 无法打印变量值并显示“错误:对 'id' 的引用不明确”)。这个问题的公认答案是,LLDB 坏了,我应该只使用 GDB;然而,我拒绝接受如此基本的东西被打破了。

Nevertheless, I've also tried using GDB with similar commands as above; however, I can't get GDB to work either. Help please

尽管如此,我也尝试过使用 GDB 和上述类似的命令;但是,我也无法让 GDB 工作。请帮忙

回答by pasawaya

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:

放置断点后,运行,程序在断点处停止,将光标悬停在要查看的变量/值上,如下所示:

enter image description here

在此处输入图片说明

You could also place an NSLog(@"%@", yourLabel.text);to view the contents of that label/other object type.

您还可以放置NSLog(@"%@", yourLabel.text);以查看该标签/其他对象类型的内容。

One other option is to run GDB in the console like this:

另一种选择是在控制台中运行 GDB,如下所示:

gdb
attach <your process name>

And then use the po(print-object) command to view the value of a variable like this:

然后使用po(print-object) 命令查看变量的值,如下所示:

po variableName

To view the value of primitive types (int, float, long, double, char, etc.), you can just use the printcommand while running GDB in the console like this:

要查看原始类型(int, float, long, double, char, 等)的值,您可以print在控制台中运行 GDB 时使用该命令,如下所示:

print yourPrimitiveVariable

Hope this helps!

希望这可以帮助!

EDIT:

编辑:

With the pocommand, 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:

使用该po命令,您可以使用属性名称 ( self.myProperty) 或 ivar 名称(可能_myProperty)打印出对象的值。我在这里演示:

enter image description here

在此处输入图片说明

回答by Bhavik Modi

Try with following expression in debug area to print object,

尝试在调试区使用以下表达式打印对象,

p self.view.bounds.size.width

or use,

或使用,

po self.view

p - Print is only uses to print normal/simple values while, po - Print Object works same as NSLog to print value of an object

p - 打印仅用于打印普通/简单值,而 po - 打印对象的工作方式与 NSLog 相同,用于打印对象的值