简体   繁体   English

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

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

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. 我知道这听起来像是一个愚蠢的问题,因为它在大多数现代IDE中都是微不足道的功能,但是我正致力于iOS开发,并且熟悉平台和SDK,因此无法解决。 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. 我在MS Visual Studio上有大量的经验,我发现Xcode的工作原理几乎相同,尽管相比而言,它仍然缺少很多功能,但仍然是一个非常好的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. 当涉及调试时,Xcode的工作方式与Visual Studio非常相似,因为您可以将鼠标悬停在变量上,它会显示其当前值。 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. 但是,当涉及对象变量时,它几乎总是只给出地址以及扩展箭头,该箭头扩展为“ NSObject”,扩展为“ isa”,扩展为所有不告诉我的属性。 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. 我已经习惯了IDE,就像Visual Studio一样,足够聪明,可以进行一些自省,并为我显示实际对象及其所有直接属性和值。 I'm assuming Xcode is smart enough to do this and I'm just not using it correctly. 我假设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 (ie 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

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. 像NSArray一样,它们是对象数组。 The debugger doesn't know what type of objects specifically, and that's due to the Objective-C language, not the debugger. 调试器不知道具体是什么类型的对象,这是由于使用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. 当您深入研究iOS开发时,我认为您会发现Apple在开发方面落后于竞争对手约15年。 That's no joke. 那不是开玩笑。 But they're catching up and, trust me, it used to be much worse! 但是他们正在追赶,并且相信我,它曾经变得更糟!

For my data objects I tend to overwrite the description method and output a string with the summary of the object. 对于我的数据对象,我倾向于覆盖description方法并输出带有对象摘要的字符串。 Something like this: 像这样:

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

This means that you can quickly inspect an object in the debugger or when you NSLog it to the console. 这意味着您可以在调试器中或将其NSLog到控制台时快速检查对象。 If that description contains all relevant data you could also overwrite isEqual: so that it uses the description to compare two objects. 如果该描述包含所有相关数据,则您还可以覆盖isEqual:以便它使用该描述比较两个对象。

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 description method, you would set it to 例如,要使其调用description方法,可以将其设置为

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

Few more details here: understanding Xcode debugger var display 这里没有更多详细信息: 了解Xcode调试器var显示

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 -description method to the console. 如果单击该按钮,它将把其-description方法的输出输出到控制台。

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 =(类),而不是能够找出对象是什么。

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). 甚至已经过去了几年,苹果仍然落后,因为竞争在他们周围不断进行(现在,他们比竞争对手目前的状态落后了16年)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM