ios Xcode Debugger:查看变量的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4735156/
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-08-30 18:34:58  来源:igfitidea点击:

Xcode Debugger: view value of variable

iphoneobjective-cxcodedebuggingios

提问by Manni

My code in a UITableViewController:

我在 UITableViewController 中的代码:

delegate.myData = [myData objectAtIndex:indexPath.row];

How can I see the values of delegate.myDataor indexPath.rowin the Debugger? delegate.myDatashould be an array and indexPath.rowan int. I can only see memory addresses of the objects delegateand indexPathbut where are myDataand row?

如何查看调试器中的delegate.myData或的值indexPath.rowdelegate.myData应该是一个数组和indexPath.row一个int. 我只能看到物体的内存地址delegate,并indexPath在哪儿呢?myDatarow

alt text

替代文字

回答by Andriy

Check this How to view contents of NSDictionary variable in Xcode debugger?

检查这个如何在 Xcode 调试器中查看 NSDictionary 变量的内容?

I also use

我也用

po variableName
print variableName

in Console.

在控制台中。

In your case it is possible to execute

在您的情况下,可以执行

print [myData objectAtIndex:indexPath.row]  

or

或者

po [myData objectAtIndex:indexPath.row]

回答by Lex L.

I agree with other posters that Xcode as a developing environment should include an easy way to debug variables. Well, good news, there IS one!

我同意其他海报,Xcode 作为开发环境应该包括调试变量的简单方法。好吧,好消息,有一个!

After searching and not finding a simple answer/tutorial on how to debug variables in Xcode I went to explore with Xcode itself and found this (at least for me) very useful discovery.

在搜索并没有找到关于如何在 Xcode 中调试变量的简单答案/教程后,我去探索了 Xcode 本身,发现这个(至少对我而言)非常有用的发现。

How to easily debug your variables in Xcode 4.6.3

如何在 Xcode 4.6.3 中轻松调试变量

In the main screen of Xcode make sure to see the bottom Debug Area by clicking the upper-right corner button showed in the screenshot.

在 Xcode 的主屏幕中,通过单击屏幕截图中显示的右上角按钮确保看到底部的调试区域。

Debug Area button

调试区按钮

Debug Area in Xcode 4.6.3

Xcode 4.6.3 中的调试区

Now set a Breakpoint – the line in your code where you want your program to pause, by clicking the border of your Code Area.

现在设置一个断点 - 通过单击代码区域的边框,您希望程序暂停的代码行。

Breakpoint

断点

Now in the Debug Area look for this buttons and click the one in the middle. You will notice your area is now divided in two.

现在在调试区域中查找此按钮并单击中间的那个。您会注意到您的区域现在被一分为二。

Split Debug Area

拆分调试区

Should look like this

应该是这样的

Now run your application.

现在运行您的应用程序。

When the first Breakpoint is reached during the execution of your program you will see on the left side all your variables available at that breakpoint.

在程序执行期间到达第一个断点时,您将在左侧看到该断点处可用的所有变量。

Search Field

搜索字段

You can expand the left arrows on the variable for a greater detail. And even use the search field to isolate that variable you want and see it change on real time as you "Step into" the scope of the Breakpoint.

您可以展开变量上的左箭头以获得更多详细信息。甚至使用搜索字段来隔离您想要的变量,并在您“进入”断点范围时实时查看它的变化。

Step Into

踏入

On the right side of your Debug Area you can send to print the variables as you desire using the mouse's right-button click over the desired variable.

在调试区域的右侧,您可以使用鼠标右键单击所需的变量,根据需要发送以打印变量。

Contextual Menu

上下文菜单

As you can see, that contextual menu is full of very interesting debugging options. Such as Watchthat has been already suggested with typed commands or even Edit Value…that changes the runtime value of your variable!

如您所见,该上下文菜单充满了非常有趣的调试选项。例如已经使用键入命令或什至编辑值建议的Watch ……它会更改变量的运行时值!

回答by LightMan

Also you can:

你也可以:

  1. Set a breakpoint to pause the execution.
  2. The object must be inside the execution scope
  3. Move the mouse pointer over the object or variable
  4. A yellow tooltip will appear
  5. Move the mouse over the tooltip
  6. Click over the two little arrows pointing up and down
  7. A context menu will pop up
  8. Select "Print Description", it will execute a [object description]
  9. The description will appear in the console's output
  1. 设置断点以暂停执行。
  2. 对象必须在执行范围内
  3. 将鼠标指针移到对象或变量上
  4. 将出现黄色工具提示
  5. 将鼠标移到工具提示上
  6. 单击向上和向下的两个小箭头
  7. 将弹出一个上下文菜单
  8. 选择“打印描述”,它将执行一个[对象描述]
  9. 描述将出现在控制台的输出中

IMHO a little bit hidden and cumbersome...

恕我直言,有点隐藏和麻烦......

回答by LightMan

Your confusion stems from the fact that declared properties are not (necessarily named the same as) (instance) variables.

您的困惑源于这样一个事实,即声明的属性不是(必须命名为相同)(实例)变量。

The expresion

表达式

indexPath.row

is equivalent to

相当于

[indexPath row]

and the assignment

和任务

delegate.myData = [myData objectAtIndex:indexPath.row];

is equivalent to

相当于

[delegate setMyData:[myData objectAtIndex:[indexPath row]]];

assuming standard naming for synthesised properties.

假设合成属性的标准命名。

Furthermore, delegateis probably declared as being of type id<SomeProtocol>, i.e., the compiler hasn't been able to provide actual type information for delegateat that point, and the debugger is relying on information provided at compile-time. Since idis a generic type, there's no compile-time information about the instance variables in delegate.

此外,delegate可能是声明为类型的id<SomeProtocol>,即,编译器一直未能为提供实际的类型信息,delegate在这一点上,和调试器是依靠在编译时提供的信息。由于id是泛型类型,因此没有关于delegate.

Those are the reasons why you don't see myDataor rowas variables.

这些就是您没有看到myDatarow作为变量的原因。

If you want to inspect the result of sending -rowor -myData, you can use commands por po:

如果要检查发送-rowor的结果-myData,可以使用命令por po

p (NSInteger)[indexPath row]
po [delegate myData]

or use the expressions window (for instance, if you know your delegateis of actual type MyClass *, you can add an expression (MyClass *)delegate, or right-click delegate, choose View Value as…and type the actual type of delegate(e.g. MyClass *).

或使用表达式窗口(例如,如果您知道您delegate的实际类型MyClass *,您可以添加一个表达式(MyClass *)delegate,或右键单击delegate,选择View Value as…并输入实际类型delegate(例如MyClass *)。

That being said, I agree that the debugger could be more helpful:

话虽如此,我同意调试器可能更有帮助:

  • There could be an option to tell the debugger window to use run-time type information instead of compile-time information. It'd slow down the debugger, granted, but would provide useful information;

  • Declared properties could be shown up in a group called properties and allow for (optional) inspection directly in the debugger window. This would also slow down the debugger because of the need to send a message/execute a method in order to get information, but would provide useful information, too.

  • 可以有一个选项告诉调试器窗口使用运行时类型信息而不是编译时信息。它会减慢调试器的速度,当然,但会提供有用的信息;

  • 声明的属性可以显示在称为属性的组中,并允许直接在调试器窗口中进行(可选)检查。由于需要发送消息/执行方法以获取信息,这也会减慢调试器的速度,但也会提供有用的信息。

回答by Jayprakash Dubey

You can print values onto console window at run-time. Below are steps :

您可以在运行时将值打印到控制台窗口。以下是步骤:

  1. Place a break-point for which you want to get values
  2. Now perform step-by-step debug.
  3. Place a cursor on variable/delegate whose value is to be checked at run-time.
  4. Now this will show description of variable/delegate
  5. Clicking on "i" will show detailed description
  6. This will also print details onto console window.
  1. 放置一个要获取其值的断点
  2. 现在执行逐步调试。
  3. 将光标放在要在运行时检查其值的变量/委托上。
  4. 现在这将显示变量/委托的描述
  5. 单击“i”将显示详细说明
  6. 这还将在控制台窗口上打印详细信息。

Screenshot for printing details on console window

在控制台窗口上打印详细信息的屏幕截图

回答by Aurum Aquila

This gets a little complicated. These objects are custom classes or structs, and looking inside them is not as easy on Xcode as in other development environments.

这有点复杂。这些对象是自定义类或结构,在 Xcode 上查看它们的内部并不像在其他开发环境中那么容易。

If I were you, I'd NSLog the values you want to see, with some description.

如果我是你,我会 NSLog 你想看到的值,并附上一些描述。

i.e:

IE:

NSLog(@"Description of object & time: %i", indexPath.row);

回答by tbone

Try Run->Show->Expressions

尝试运行->显示->表达式

Enter in the name of the array or whatever you're looking for.

输入数组的名称或您要查找的任何名称。