How to set a conditional breakpoint in Xcode based on an object string property?

Coocoo4Cocoa picture Coocoo4Cocoa · Jun 12, 2009 · Viewed 57.2k times · Source

I'm looking to be able to have the debugger break when it reaches a particular string match. As an example, I might have something like this:

Foo myObj = [self gimmeObj];

myObj might have a property called name. I want the debugger to stop on the assignment when

[myObj.name isEqualToString:@"Bar"];

How can I set my conditional breakpoint in Xcode to do that?

Answer

Peter N Lewis picture Peter N Lewis · Jun 13, 2009

You can set a conditional break point in Xcode by setting the breakpoint normally, then control-click on it and select Edit Breakpoint (choose Run -> Show -> Breakpoints).

In the breakpoint entry, there is a Condition column.

Now, there are several issues to keep in mind for the condition. Firstly, gdb does not understand dot syntax, so instead of myObj.name, you must use [myObj name] (unless name is an ivar).

Next, as with most expressions in gdb, you must tell it the type of return result, namely "BOOL". So set a condition like:

(BOOL)[[myObj name] isEqualToString:@"Bar"]

Often it is actually easier to just do this in code by temporarily adding code like:

if ( [myObj.name isEqualToString:@"Bar"] ) {
    NSLog( @"here" );
}

and then setting the break point on the NSLog. Then your condition can be arbitrarily complex without having to worry about what gdb can and can't parse.