Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access self or ivars from a block when debugging

In the debugger (gdb and llvm),

I usually do:

po self
po myIvar
p (CGPoint)whatEver

and works fine except when I am inside of a block. How can I access them in the debugger? I don't like very much writing NSLogs everywhere ...

I suppose inside blocks In the debugger I need to access ivars in a different way but I don't know how :(

like image 355
nacho4d Avatar asked Mar 01 '12 10:03

nacho4d


1 Answers

Blocks are their own environment when they're executed. The neat thing about them is that they'll capture any variables from the surrounding scope that you mention in their bodies. The flip side of that is that there's no access to variables that aren't captured.

Take a look at this snippet:

NSArray * a = [NSArray array];
NSDictionary * d = [NSDictionary dictionary];
NSString * s = @"This is my string. There are many others like it.";

void (^myB)(NSInteger) = ^(NSInteger i){
    NSString * lS = [s lowercaseString];
    lS = [lS stringByReplacingOccurrencesOfString:@"many" withString:[NSString stringWithFormat:@"%ld", i]];
/* Breakpoint here */    NSLog(@"%@", lS);
};

myB(7);

The Block captures s and uses it. The NSInteger parameter, i, is also used and accessible inside the Block. The breakpoint gets hit when the Block is executed, though, which means that the creating scope, with the array a and dictionary d, no longer exists. You can see this if you look at the local variable display in Xcode:

Local variable display of the Block

Aside from globals, that's all you or the debugger have access to when the Block is executing. If you really need to know the values of other variables during that time, I think you'll have to mention them inside the Block. This will capture them, which will mean (for objects) they'll be retained and then released when the Block is deallocated.

like image 173
jscs Avatar answered Nov 15 '22 22:11

jscs