8

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.

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

enter image description here

devios1
  • 36,899
  • 45
  • 162
  • 260

3 Answers3

8

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().

jscs
  • 63,694
  • 13
  • 151
  • 195
  • Ok that is definitely a start. I can at least see what the Hours objects are now. It's unfortunate the default NSObject implementation can't just recursively show properties and values in a generic way. I guess Obj-C is too low level for that kind of stuff. – devios1 Feb 27 '13 at 00:47
  • 1
    There are introspection facilities in the runtime library, so it's actually feasible for an object to just spit out all its properties, no matter what they are: http://www.google.com/search?q=site:stackoverflow.com+-site:meta.stackoverflow.com+objective-c+print+all+properties+of+an+object – jscs Feb 27 '13 at 00:53
  • 2
    @chaiguy have a look at that super useful answer from kendall-helmstetter-gelner on proper introspection (which is what you are asking for); http://stackoverflow.com/a/2304797/91282 – Till Feb 27 '13 at 00:53
  • @JoshCaswell, Till Awesome that's good to know. Perhaps I can make use of the runtime introspection in some kind of reusable manner in my project. – devios1 Feb 27 '13 at 01:00
1

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.

Mark Bernstein
  • 2,090
  • 18
  • 23
  • See that's what I would have thought too, but it's *not* empty, and when debugging, the for..in loop iterates over 7 objects, but I can't see that there are 7 objects unless I explicitly `po` storeHours in the console. Very confusing. – devios1 Feb 27 '13 at 00:37
  • Are you on the latest version of Xcode, @chaiguy? LLDB was a bit buggy at the beginning with respect to viewing ivars. – jscs Feb 27 '13 at 00:40
  • It's the latest I think (4.5.2 (4G2008a)). The console says (gdb) though so presumably that means I'm not using LLDB? – devios1 Feb 27 '13 at 00:41
0

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.
}
Saran
  • 6,274
  • 3
  • 39
  • 48