调试时在 Xcode 中查看对象/属性状态

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

View object/property state in Xcode while debugging

iosobjective-cxcodedebugging

提问by devios1

So I'm new to Xcode, working on an iOS project and I'm having a heck of a time with the most fundamental debugging. Specifically I need to be able to view the state of objects as I step through the code (that's not crazy is it?), but I can't for the life of me figure out how to do this in Xcode.

所以我是 Xcode 的新手,正在从事一个 iOS 项目,而且我有很多时间进行最基本的调试。具体来说,我需要能够在我逐步执行代码时查看对象的状态(这并不疯狂,是吗?),但我一生都无法弄清楚如何在 Xcode 中执行此操作。

Whenever I try, it seems the furthest I get is a memory address that can't be expanded to show its objective contents. Nor can I figure out any way to even manually dereference the pointer in the debug console to view the state of that object.

每当我尝试时,似乎我得到的最远的是一个无法扩展以显示其客观内容的内存地址。我也无法想出任何方法来手动取消对调试控制台中的指针的引用以查看该对象的状态。

Here I am trying to view the contents of the store.storeHoursarray, with no luck whatsoever. In fact the view on the left tells me there are 0 objects in the array, and won't show anything when I try to expand it, but when I po store.storeHoursthe console shows 7 objects, albeit uselessly portrayed as memory addresses.

在这里,我试图查看store.storeHours数组的内容,但没有任何运气。事实上,左边的视图告诉我数组中有 0 个对象,当我尝试扩展它时不会显示任何内容,但是当我po store.storeHours控制台显示 7 个对象时,尽管被无用地描绘为内存地址。

enter image description here

在此处输入图片说明

Please tell me I'm not crazy and I'm just missing something!

请告诉我我没有疯,我只是错过了一些东西!

Update:So things get even weirder! When I switch the variable display to "Local" instead of "Auto" suddenly, self.store.storeHoursbecomes fully navigable! I'm wondering if perhaps there was a glitch accessing the correct "storeHours" instance or something because it's clearly identifying 7 objects in the array when I view it now! Not to mention the objects are expandable as I was originally hoping.

更新:所以事情变得更奇怪了!当我突然将变量显示切换到“本地”而不是“自动”时,self.store.storeHours变得完全可导航!我想知道是否可能在访问正确的“storeHours”实例时出现故障或其他原因,因为当我现在查看它时它清楚地识别了数组中的 7 个对象!更不用说对象是可扩展的,正如我最初希望的那样。

enter image description here

在此处输入图片说明

采纳答案by jscs

The instances are actually providing that information themselves. You need to implement the descriptionmethod, which is inherited from NSObject, for your custom classes in order for them to be able to print themselves as something other than a memory address (which is what NSObject's implementation does).

这些实例实际上自己提供了这些信息。您需要为您的自定义类实现description从 继承的方法,NSObject以便它们能够将自己打印为内存地址以外的其他内容(这就是NSObject的实现所做的)。

I've no idea what properties your Hoursclass has, but this is as simple as something like:

我不知道你的Hours类有什么属性,但这很简单:

- (NSString *)description
{
    return [NSString stringWithFormat:@"Open: %i Close: %i", self.openTime, self.closeTime];
}

The method just needs to return an NSStringcontaining whatever information you think is important to see when inspecting the object.

该方法只需要返回一个NSString包含任何您认为在检查对象时很重要的信息。

This is also how classes represent themselves when you use the %@format specifier in NSLog().

这也是当您%@NSLog().

回答by Mark Bernstein

In your example, store.storeHours is an empty NSArray. So, naturally, you can't look inside it.

在您的示例中,store.storeHours 是一个空的 NSArray。所以,自然,你不能看里面。

For more clarity in the debugger, try adding a method (inherited from NSObject)

为了在调试器中更清晰,尝试添加一个方法(从 NSObject 继承)

 - (NSString*) description

to your objects like Hoursthat tells you more about their contents. See also debugDescription.

Hours这样的对象会告诉您更多有关其内容的信息。另见debugDescription

回答by Saran

implement

实施

-(NSString*)description{
    //Return a string in whatever way you like to describe this instance. That is what xcode debugger reads. 
    //This is implemented in the parent to return the address, that's why you see that way.
}