在 Xcode 调试中观察变量或检查对象的技巧

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

tips on watching variables or examining objects in Xcode debugging

iosxcodedebugging

提问by MonkeyBonkey

I'm coming over to Xcode from visual studio and am having some trouble inspecting or examining variables at my breakpoints.

我从 Visual Studio 转到 Xcode,但在我的断点处检查或检查变量时遇到了一些麻烦。

Often times I'll try to examine an object by mouse-over and and expand out it's properties and I'll only see a hexadecimal representation of the attributes and nothing in the summary in a more human readable form. For example, the Facebook object in the Facebook sdk has an access token property that is an NSCFString. In the debugger how can I see the actual string value rather than the hex value for that attribute?

很多时候,我会尝试通过鼠标悬停来检查一个对象并展开它的属性,我只会看到属性的十六进制表示,而摘要中没有任何更易读的形式。例如,Facebook sdk 中的 Facebook 对象有一个访问令牌属性,它是一个 NSCFString。在调试器中,我如何才能看到该属性的实际字符串值而不是十六进制值?

Are there any very helpful gdb commands that I should use in the debug window?

我应该在调试窗口中使用任何非常有用的 gdb 命令吗?

Also, why does it always say add to iTunes as a spoken track when I mouse over things in the debugger?

另外,为什么当我将鼠标悬停在调试器中的内容上时,它总是说添加到 iTunes 作为语音曲目?

回答by Faizan S.

As you pointed out, there are some GDB commands to enlighten your life.

正如您所指出的,有一些 GDB 命令可以启发您的生活。

Print Object:

打印对象:

po objectName

对象名称

Is probably the most used command to help you with debugger inside Xcode. Here's a roundup on GDB commands inside the Xcode debugger : Debugging tips for Objective-C programming

可能是最常用的命令,可以帮助您使用 Xcode 中的调试器。这是 Xcode 调试器中 GDB 命令的综述:Objective-C 编程的调试技巧

Personally I kind of rely on the old-fashioned print statements using NSLog. It really is a painto use the GDB commands all the time.

我个人有点依赖使用 NSLog 的老式打印语句。这真的是一个痛苦使用GDB命令所有的时间

To me things are faster when I can just print the object and actually see what's going on everytime I run the application:

对我来说,当我可以打印对象并在每次运行应用程序时实际查看发生了什么时,事情会更快:

NSLog(@"%@", [info valueForKey:key]);

Here are some tips on using NSLog with parameters: CocoaDev: NSLog

这里有一些关于使用带参数的 NSLog 的提示:CocoaDev: NSLog

Downpoint: I have to clear out the NSLogs once the app is going to be released.

缺点:一旦应用程序要发布,我必须清除 NSLogs。



To your second rather odd iTunes question: Have you enabled accessibility mode on your Mac? (Just a guess, I have never come across something like that)

对于您的第二个奇怪的 iTunes 问题:您是否在 Mac 上启用了辅助功能模式?(只是猜测,我从来没有遇到过这样的事情)

回答by j yrez

Your iTunes question: Maybe you're accidentally picking up something from data tips.

您的 iTunes 问题:也许您不小心从数据提示中获取了一些信息。

I'm having trouble with data tips not working in OSX 5.1.1: hover doesn't display and there's no eye and info button. Instead, clicking on a variable drops down a menu that ends with Add to iTunes..., which is what I'm guessing you're seeing somehow.

我在 OSX 5.1.1 中无法使用数据提示时遇到问题:不显示悬停并且没有眼睛和信息按钮。相反,单击一个变量会下拉一个以“添加到 iTunes...”结尾的菜单,我猜这就是您所看到的。

I'm sorry I can't be clearer, but I haven't been able to figure out how this menu relates to data tips that don't work. I have posted a question on how to fix it (xcode 5.1.1 data tips fails in debugging), and if I get answers that apply to your question I will share them with you.

很抱歉,我无法说得更清楚,但我一直无法弄清楚此菜单与不起作用的数据提示有何关联。我已经发布了一个关于如何修复它的问题(xcode 5.1.1 数据提示在调试中失败),如果我得到适用于您的问题的答案,我会与您分享。