如何在 Xcode 调试器中查看对象内的值?

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

How can I view the values inside an object in the Xcode debugger?

iphonexcodeipaddebuggingios

提问by BeachRunnerFred

I know that might sound like a stupid question since it's a trivial feature in most modern IDEs, but I'm diving into iOS development and am getting familiar with the platform and SDK and I can't figure this out. I have a ton of experience with MS Visual Studio and I see that Xcode works very much the same, tho it's still lacking quite a bit of functionality in comparison, but still a very good IDE. When it comes to debugging, Xcode works very similar to Visual Studio in that you can hover your mouse over a variable and it will display its current value. When it comes to object variables, however, it almost always give just the address along with the expand arrow, which expands to "NSObject", which expands to "isa", which expands to all the attributes that don't tell me anything. I'm used to the IDE, like Visual Studio, being smart enough to do some introspection and display for me the actual object and all it's immediate properties and values. I'm assuming Xcode is smart enough to do this and I'm just not using it correctly.

我知道这可能听起来像一个愚蠢的问题,因为它是大多数现代 IDE 中的一个微不足道的功能,但我正在深入研究 iOS 开发并且正在熟悉平台和 SDK,但我无法弄清楚这一点。我在 MS Visual Studio 方面有很多经验,我发现 Xcode 的工作原理非常相似,但相比之下,它仍然缺乏相当多的功能,但仍然是一个非常好的 IDE。在调试方面,Xcode 的工作方式与 Visual Studio 非常相似,您可以将鼠标悬停在变量上,它会显示其当前值。然而,当涉及到对象变量时,它几乎总是只给出地址和扩展箭头,它扩展为“NSObject”,它扩展为“isa”,它扩展到所有不告诉我任何内容的属性。我习惯了IDE,像 Visual Studio 一样,足够聪明,可以进行一些内省并为我显示实际对象及其所有直接属性和值。我假设 Xcode 足够聪明,可以做到这一点,但我只是没有正确使用它。

If I set a breakpoint on a line of code that involves an object instance (lets say NSDateComponents instance), how can I view the values of its properties (i.e. year, week, day, hour, etc.)?

如果我在涉及对象实例(比如 NSDateComponents 实例)的代码行上设置断点,我如何查看其属性的值(即年、周、日、小时等)?

Thanks in advance for your help!

在此先感谢您的帮助!

Edit:Here's a screenshot of the info I get with every object I inspect... alt text http://joecrotchett.com/images/misc/example.jpg

编辑:这是我检查的每个对象获得的信息的屏幕截图... 替代文本 http://joecrotchett.com/images/misc/example.jpg

采纳答案by beeeefy

It's frustrating. The debugger has to know the structure of every object and it apparently doesn't. It used to be much worse, though. Like with NSArray's, they're an array of objects. The debugger doesn't know what type of objects specifically, and that's due to the Objective-C language, not the debugger.

这令人沮丧。调试器必须知道每个对象的结构,而它显然不知道。不过,它曾经更糟。与 NSArray 一样,它们是一个对象数组。调试器不知道具体是什么类型的对象,这是由于 Objective-C 语言,而不是调试器。

As you dive into iOS development, I think you're going to find that Apple is about 15 years behind its competitors when it comes to development. That's no joke. But they're catching up and, trust me, it used to be much worse!

当您深入研究 iOS 开发时,我想您会发现 Apple 在开发方面落后于竞争对手大约 15 年。这不是开玩笑。但他们正在迎头赶上,相信我,它曾经更糟!

回答by Besi

For my data objects I tend to overwrite the descriptionmethod and output a string with the summary of the object. Something like this:

对于我的数据对象,我倾向于覆盖该description方法并输出一个带有对象摘要的字符串。像这样的东西:

Printing description of myContact:
{name=Homer, lastname=Simpson, ...}

This means that you can quickly inspect an object in the debugger or when you NSLogit to the console. If that description contains all relevant data you could also overwrite isEqual:so that it uses the description to compare two objects.

这意味着您可以在调试器中或在NSLog控制台中快速检查对象。如果该描述包含所有相关数据,您也可以覆盖,isEqual:以便它使用描述来比较两个对象。

回答by Bryan

You can also change the way the Xcode shows a value in the local variables pane by going to "Edit Summary Format" on the right-click menu.

您还可以通过转到右键单击菜单上的“编辑摘要格式”来更改 Xcode 在局部变量窗格中显示值的方式。

For example, to get it to call the descriptionmethod, you would set it to

例如,要让它调用该description方法,您可以将其设置为

{(NSString*)[$VAR description]}:s

Few more details here: understanding Xcode debugger var display

这里有更多细节: 了解 Xcode 调试器 var 显示

回答by Dan Ray

Hover over a variable and a line will pop up about it. Move your mouse right very slightly, and hover over the triangle arrow that's at the left side of the popup, that arrow will turn downward, and another popup will open showing all the named properties of that object.

将鼠标悬停在一个变量上,会弹出一条关于它的行。轻轻向右移动鼠标,将鼠标悬停在弹出窗口左侧的三角形箭头上,该箭头将向下转动,另一个弹出窗口将打开,显示该对象的所有命名属性。

You can also get it to print its "description" value. Move your pointer slightly right until it's over the two little "up/down" arrows. Then left (well, "main") click. A pop-up will show up and one option will be "Print description". If you click on that, it'll put the output of its -descriptionmethod to the console.

你也可以让它打印它的“描述”值。将指针稍微向右移动,直到它位于两个小的“向上/向下”箭头上方。然后左键(好吧,“主要”)单击。将出现一个弹出窗口,其中一个选项是“打印说明”。如果您单击它,它会将其-description方法的输出放到控制台。

回答by kindaian

Well, a couple more years have passed, and xcode still is showing isa = (Class) on most of the objects instead of be able to figure out what is the object.

好吧,几年过去了,xcode 仍然在大多数对象上显示 isa = (Class) 而不是能够弄清楚对象是什么。

And even that a couple of years have passed, Apple is still behind, because the competition is running loops around them (and they are now 16 years behind of competitores current state).

即使已经过去了几年,Apple 仍然落后,因为竞争正在围绕他们运行(他们现在比竞争对手的当前状态落后 16 年)。