View object/property state in Xcode while debugging

本秂侑毒 提交于 2020-02-03 04:44:01

问题


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.

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.storeHours array, 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.storeHours the console shows 7 objects, albeit uselessly portrayed as memory addresses.

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.storeHours becomes 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.


回答1:


The instances are actually providing that information themselves. You need to implement the description method, 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).

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

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

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

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




回答2:


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

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

 - (NSString*) description

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




回答3:


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.
}


来源:https://stackoverflow.com/questions/15102082/view-object-property-state-in-xcode-while-debugging

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!