你如何在 xcode 中观察或评估一个表达式,比如 Visual Studio 的直接窗口?

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

How do you watch or evaluate an expression in xcode like visual studio's immediate window?

objective-cxcodedebugging

提问by user4951

In MS visual studio we just right click add watch.

在 MS Visual Studio 中,我们只需右键单击添加手表。

How does one do this in Xcode?

在 Xcode 中如何做到这一点?

回答by Gabe Prime

Use the pocommand in the Debug area

使用poDebug区的命令

Set up a breakpoint on the relevant area of code, then when the program stops at the breakpoint, enter commands in the Console in the Debug Area. The relevant command is po(print object) followed by the expression you want to evaluate.

在代码的相关区域设置断点,然后当程序在断点处停止时,在调试区的控制台中输入命令。相关命令是po( print object) 后跟要计算的表达式。

If the Debug window is not visible in XCode, you can show it via the top menu:

如果调试窗口在 XCode 中不可见,您可以通过顶部菜单显示它:

'View' -> 'Debug Area' -> 'Activate Console'(XCode v8.x)

'View' -> 'Debug Area' -> 'Activate Console'( XCode v8.x)

Example

例子

To evaluate an expression like var1/var2where var1and var2are both doubles, enter the following in the Console:

要评估像var1/var2wherevar1var2are doubles这样的表达式,请在控制台中输入以下内容:

po var1/var2

The Console will return something like:

控制台将返回如下内容:

(double)  = 3.085 [no Objective-C description available]

Showing object properties

显示对象属性

You can also return a particular property of an object currently used in the code at that breakpoint:

您还可以在该断点处返回代码中当前使用的对象的特定属性:

po [bankInfo city]

And it will return something like:

它会返回类似的东西:

(id)  = 0x000069e8 Testville

Note though that the Console doesn't seem to like the dot notation and prefers the square brackets when applicable. For example, this returns an error for me:

请注意,控制台似乎不喜欢点符号,并且在适用时更喜欢方括号。例如,这对我来说返回一个错误:

po bankInfo.city

I hope this is what you've been looking for.

我希望这就是你一直在寻找的。

回答by gideon

Gabe's answer is almost there but missing one crucial detail: Select Debugger Output. By default the bottom option is set to Target Output, so the pocommand doesn't show you anything.

Gabe 的回答差不多了,但遗漏了一个关键细节:选择Debugger Output。默认情况下,底部选项设置为目标输出,因此该po命令不会向您显示任何内容。

Here is a sandwich appfrom a tutorial I'm debugging:

这是我正在调试的教程中的三明治应用程序

eval expression screenshot in xcode

xcode 中的 eval 表达式截图

Being an xcode newbie and coming from a MS Visual Studio Background, I wanted exactly what the OP was looking for. While playing around from reading Gabe's answer I selected Debugger Outputand got what I wanted.

作为一个 xcode 新手并且来自 MS Visual Studio 背景,我想要的正是 OP 正在寻找的东西。在阅读 Gabe 的答案时,我选择Debugger Output并得到了我想要的。

回答by Aravindhan

My seniors told to use NSLog(@variable)..........

我的前辈告诉我使用 NSLog(@variable).........

回答by Angus

Set some breakpoints in the begginning of the looping and functions. Once u click on the breakpoint(one similar to arrow) button in the editor window the "Build and debug tool" will get enabled. You can then go to the debugger by clicking the debugger icon. on the right of the debugger window variables will be visible select self->then the instance variable u r going to set watch point.Right click on that and select "watch variable".A trigger point will be set and you will be notified with the value of the variable when changed.

在循环和函数的开头设置一些断点。一旦你点击编辑器窗口中的断点(一个类似于箭头的)按钮,“构建和调试工具”将被启用。然后,您可以通过单击调试器图标转到调试器。在调试器窗口的右侧变量将是可见的选择 self-> 然后实例变量 ur 要设置观察点。右键单击它并选择“观察变量”。将设置触发点,您将收到通知更改时变量的值。

回答by Pfitz

As fas as i understand you would like to see when a variable is changing. For this make a breakpoint and right click on it and choose Edit Breakpoint. A window will appear:

据我所知,您希望看到变量何时发生变化。为此,创建一个断点并右键单击它并选择Edit Breakpoint。会出现一个窗口:

enter image description here

在此处输入图片说明

Make sure you choose the right actionlike Debugger Commandor Log Messageand check the tick down at the options Automatically continue after evaluating. So you get some kind of action (e.g. logging, sound, etc) and wont stop at the breakpoint.

确保您选择了正确的操作,例如Debugger CommandLog Message并检查选项处的勾号Automatically continue after evaluating。因此,您会得到某种操作(例如日志记录、声音等)并且不会在断点处停止。

回答by Rob

If you want to know when the variable changes, use a "watch":

如果您想知道变量何时发生变化,请使用“监视”:

  1. Set a breakpoint somewhere in the class in question;
  2. Run the app in the debugger and let it stop at your breakpoint; and
  3. Go to the "Variables" view in the left side of the bottom "Debug" panel and right click on the property in question and choose "Watch".
  1. 在有问题的类中的某个地方设置一个断点;
  2. 在调试器中运行应用程序,让它在你的断点处停止;和
  3. 转到底部“调试”面板左侧的“变量”视图,右键单击相关属性并选择“观察”。

For example, here, I've stopped at a breakpoint in viewDidLoad, and added a "watch" for total:

例如,在这里,我在 中的断点处停止viewDidLoad,并添加了一个“观察” total

Watch

手表

(This is Swift, but the same is true for Objective-C, too.)

(这是 Swift,但同样适用于 Objective-C。)

Now, if I "continue" execution (enter image description here), Xcode will pause whenever this property changes and I can see at what line of code totalis changing, the stack trace to get to that point, etc.

现在,如果我“继续”执行 ( 在此处输入图片说明),Xcode 将在此属性发生更改时暂停,并且我可以看到代码行total发生更改、到达该点的堆栈跟踪等。