ios 在 XCode 中调试 - 通过代码和断点运行

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

Debugging in XCode - running through code and breakpoints

iosxcodeios-simulator

提问by StuartM

I would like to know how to debug best in xcode. I know how to set a break point but ideally i would like the simulator to stop and then step through the code...

我想知道如何在 xcode 中进行最佳调试。我知道如何设置断点,但理想情况下我希望模拟器停止然后单步执行代码...

Not step through breakpoints but step through the code line by line so I can see where it goes, what methods are run etc...

不是单步执行断点,而是逐行执行代码,这样我就可以看到它的去向,运行了哪些方法等...

Is this possible, if so how?

这是可能的,如果可以,如何?

回答by Thomson Comer

Debug control icons in Xcode

在 Xcode 中调试控件图标

At the bottom left of your code window in Xcode 4 you can see the debug step controls. Each has a short alt text to explain it. I'll explain in slightly more detail here. Learning to become an expert debugger is the subject of many textbooks.

在 Xcode 4 中代码窗口的左下角,您可以看到调试步骤控件。每个都有一个简短的替代文字来解释它。我将在这里稍微详细地解释一下。学习成为专家调试器是许多教科书的主题。

The play button alt text: Continue program execution. You've probably found this already. Pressing it advances to the next breakpoint.

播放按钮替代文字:继续程序执行。你可能已经发现了这一点。按它前进到下一个断点。

The jumping arrow alt text: Step over. Pressing this button repeatedly will allow you to advance through your program at the current level of scope just as your program would. It will allow you to observe the behavior of your program step by step through the current method. When the current method finishes, Step over will take you to the calling method one step up in the program stack.

跳跃箭头替代文字:Step over。重复按下此按钮将允许您像您的程序一样在当前范围级别推进您的程序。它将允许您通过当前方法逐步观察程序的行为。当前方法完成后,Step over 将带您到程序堆栈中上一级的调用方法。

The down arrow alt text: Step into. Pressing this button will follow the new method into its scope and allow you to view code in the called-method if it has been compiled for debugging. For example, clicking this on the current line of code in the above debug window will take you to the _generateSources method, which you can then progress through with Step over.

向下箭头替代文字:步入. 按此按钮将跟随新方法进入其范围,并允许您查看被调用方法中的代码(如果它已被编译用于调试)。例如,在上述调试窗口中的当前代码行上单击此选项将带您进入 _generateSources 方法,然后您可以通过 Step over 继续执行该方法。

The up arrow alt text: Step out. Takes you out of the current context and into the calling method one step up in the program stack. This behaves identically to finishing a method using Step over, executing the program as normal and executing all of the lines of code in the original scope that you did not debug using Step over.

向上箭头替代文字:Step out。将您带出当前上下文并进入程序堆栈中上一级的调用方法。这与使用 Step over 完成方法的行为相同,正常执行程序并在原始范围内执行您没有使用 Step over 调试的所有代码行。

View of call stack

调用堆栈视图

Click on the silhouette with the blue background to see the current call stack. The call stack will always progress all the way from your current scope to the first method that was called to launch your program. Each method you see here was called in sequence in order to reach -[HelloWorldLayer init]. When you press Step outthen the current line of executing code will return to -[CCNode node], and if you have the source for it you can browse it.

单击蓝色背景的轮廓以查看当前调用堆栈。调用堆栈将始终从您当前的范围一直进展到为启动您的程序而调用的第一个方法。您在此处看到的每个方法都按顺序调用以到达 -[HelloWorldLayer init]。当您按Step out 时,当前执行代码行将返回到 -[CCNode node],如果您有它的源代码,您可以浏览它。

To the left are the current Local variables visible from the scope of the line of code currently being executed (line 76 in this image). These variables are how you can really use the above commands like Step over. backgroundand winSizeare local variables that were defined in this scope and are currently being used. When the scope is exited they will be gone. _cmdis a pointer to the selector that is currently being invoked in Objective-C: -[HelloWorldLayer init]. You won't need this until you are an advanced debugger. selfis the pointer to an object containing all of the ivars that belong to the currently executing class, HelloWorldLayer, and objc_superis a pointer to the parent class of HelloWorldLayer.

左侧是当前正在执行的代码行范围内可见的当前局部变量(此图中的第 76 行)。这些变量是您如何真正使用上述命令(如Step over )的方法backgroundwinSize是在此范围内定义并且当前正在使用的局部变量。当范围退出时,它们将消失。 _cmd是指向当前在 Objective-C 中调用的选择器的指针:-[HelloWorldLayer init]。在您成为高级调试器之前,您将不需要它。 self是指向包含所有属于当前正在执行的类、HelloWorldLayer 和objc_super 的变量的对象的指针是一个指向父类HelloWorldLayer的。

回答by Ravi

I would add keyboard shortcuts to @Thomson Corner's answer.

我会在@Thomson Corner 的答案中添加键盘快捷键。

  • Use 'c' to continue. 's' to step, 'n' for next, 'f' for finish. I don't like operating the debugger with the mouse. Those icons are too tiny and getting the pointer on them is a pain (once in a while). I've always been a keyboard guy and it's REALLY comfortable with the keyboard.

  • You should also try using po- it prints out variable values with type ahead suggestions. Like po _varName. Makes it easy to check if a variable is nil-valued etc.,

  • You can also change your command aliases by modifying ~/.lldbinit.

  • 使用“c”继续。's' 为步进,'n' 为下一步,'f' 为完成。我不喜欢用鼠标操作调试器。这些图标太小了,让指针指向它们是一种痛苦(偶尔)。我一直是一个键盘手,键盘真的很舒服。

  • 您还应该尝试使用po- 它会打印出带有预先输入建议的变量值。像po _varName。使检查变量是否为零值等变得容易,

  • 您还可以通过修改~/.lldbinit.

Here's the official tutorial: http://lldb.llvm.org/tutorial.html. And another tutorial: http://www.informit.com/articles/article.aspx?p=1829415&seqNum=6. What I pointed out above are the most basic debugging functions programmers mostly perform. There are more options like exception breakpoints and loading programs, but I'm pretty sure they're for another day if you are just getting started.

这是官方教程:http: //lldb.llvm.org/tutorial.html。另一个教程:http: //www.informit.com/articles/article.aspx? p =1829415& seqNum=6。我上面指出的是程序员主要执行的最基本的调试功能。还有更多选项,例如异常断点和加载程序,但我很确定如果您刚刚开始,它们将在另一天使用。