tags:

views:

126

answers:

1

I have a recursive-descent tree object. I want to be able to set a breakpoint and inspect it in the Xcode debugger. Inspecting the top level works just fine. But after I go down a level or so, the debugger says the values of the ivars are out of scope. Is there any way I can keep this from happening?

EDIT:

In response to a comment --

The ivar is an object of class Expression. The nesting mostly comes from the body ivar, which is typically an NSMutableArray of expression objects. So you might have a structure like this:

-Expression
---body (2 expressions)
-----0 Expression
-------body (1 expression)
----------0 Expression
------------body [empty]
-----1 Expression
-------body [empty]

There is also a head ivar, which is an object of class Token, which in turn has some string ivars, does not nest.

The way I use the debugger -- I set a breakpoint in a method inside the Expression object. I then click on the disclosure triangle for arguments, then for self, then for the body ivar, then for expressions within the body ivar, and so on. But eventually the debugger stops telling me the values of things.

+1  A: 

I recommend adding this to your Expression class. Then you can po [expression explode] in the debugger to print the tree of an expression. A prerequisite is a proper -description method for your Expressionclass that prints out the rest of the ivars.

- (void) explodeAtLevel:(int)aLevel {
    NSMutableString* out = [[NSMutableString alloc] init];

    for (int i = 0;  i < aLevel; i++) [out appendString:@"-"];

    [out appendString:self.description];
    printf("%s\n", [out UTF8String]);
    [out release];

    for (id *subitem in body)
      if ([subitem isMemberOfClass:[Expression class]])
        [((Expression*)subitem) explodeAtLevel:(aLevel + 1)];
}


- (void) explode {
  [self explodeAtLevel:0];
}
Ortwin Gentz
This appears to be the best I can do. Though isKindOfClass is much better than isMemberOfClass. One question -- when I run that method in the debugger, I often get the message "Value can't be converted to integer." When I run the same method from code in the program, I don't get that message. Can you explain what is going on?
William Jockusch
`isKindOfClass` is indeed better if you're having subclasses of your base class. As for the debugger message, I suggest trying to comment out `[out appendString:self.description];`. If the problem goes away, it's something in your `-description` method.
Ortwin Gentz