Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

moonman239

Cancelled
Original poster
Mar 27, 2009
1,541
32
I have a BOOL variable. Let's call it Bob. I have a symbolic breakpoint at -[UIImageView setImage], and I want my program to break if - and only if - a variable named Bob is declared and is true. That is, given a piece of code like this:

Code:
-(void)methodA
{
BOOL bob = (some value);
UIImage *someImage = ...;
[imageView setImage:someImage];
}

-(void)methodB
{
UIImage *someOtherImage = ...;
[imageView setImage:someOtherImage];
}

the program should break after setImage is called in methodA, and then only if bob = true. But the program should not break in methodB.

Unfortunately, based on my experience, I predict that the following will actually happen:

If method A is called, the debugger will see a variable named "bob", and make sure it is true; if "bob" is false, then the program will not be paused.
If method B is called, the debugger will see that there is no variable named "bob" and will pause the program, since the debugger can't evaluate an undeclared variable.
 
A quick Google search using a all the following key word will get you an answer.

xcode conditional breakpoints

I liked this one http://jeffreysambells.com/2014/01/14/using-breakpoints-in-xcode
I saw that one, but I don't see how that's going to help. Jeff uses a BOOL variable in the same method in which he sets up a breakpoint. In my case, I'm telling the debugger to break at setImage, and the debugger figures out where setImage is in my code. There are multiple classes in my code that cause UIImageView's setImage to be fired, and I don't want to add one piece of code per class just to make a conditional breakpoint work.
 
If method B is called, the debugger will see that there is no variable named "bob" and will pause the program, since the debugger can't evaluate an undeclared variable.

In the condition, have you tried "(BOOL)bob != nil && (BOOL)bob == YES" ...?
 
in the method:
Code:
[imageView setImage:someImage];
if (bob) {
  NSLog(@"...");
}

Then you can set a breakpoint on the NSLog. You can definitely set conditional breakpoints, but if you're having trouble, this should be an easy fix.

-Lee

EDIT: if you set a conditional breakpoint on the LINE with setImage when Bob is true rather than a method breakpoint that should work, too.
 
Last edited:
The breakpoint on -setImage: actually breaks in the prologue of the implementation of the -setImage: method, not at the call site. It only appears like it does because you're calling a library function. Set a breakpoint on -methodA for a demonstration of this. It will break at the beginning of the method, not at the call site in whatever method calls it.

AFAIK, there is no way to refer to variables declared one stack frame up (the calling function) in a conditional breakpoint. You have to set your breakpoints in each method with a bob variable (on the line with the -setImage: call is fine) and then make the breakpoint conditional. Note that conditional breakpoints are unreasonably slow (the condition test itself is slow, because it actually breaks, tests in the interpreter, and continues if false) so if you expect -methodA to be called a lot, it may be better to roughly follow @lee1210's advice, although you don't need a log; an empty statement (just the semicolon) is fine, and you can break on that line. You may want to put it before the -setImage: call though if you're interested in tracing though that.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.