Xcode conditional breakpointsMar 1, 2011 · 2 minute read
a quick follow-up on the recent post on Xcode breakpoint actions with some more detailed examples on how to set conditional breakpoints. As before these examples are all based on Xcode 3 and the GDB debugger (am I the only hoping that Xcode 4 will get released tomorrow along with the iPad 2?)
Conditional breakpoints provide a way to more precisely control breakpoints. Instead of always breaking at a line you can specify a condition that is evaluated when the debugger reaches the breakpoint. Consider the following code fragment in a typical UITableViewController:
Suppose I am trying to debug what is happening at row 50 in a very large table. A quick and easy way to do that is to set a breakpoint on line 73 and test the value of the local row variable. The test is entered in the breakpoint view in the Condition column and simply checks if the row value has the value 50:
Now when I run the app and scroll down the table the debugger will break when it hits row 50 (actually row 51 in the table since the row index starts at 0). The condition statement can use any local variables that are currently in scope. Note that in this example the row variable is declared on line 71 and the breakpoint is set on line 73. It will not work if you set the breakpoint on line 71 since the variable is not set until line 71 is executed.
Another way to achieve the same thing but break before the row variable is set would be to set the breakpoint on row 71 and check the indexPath variable directly as follows:
It would have been possible to write the condition as “indexPath.row==50” but I wanted to illustrate the syntax for executing a function or method call. The thing to remember when using a method is that you have to tell the debugger the return type by explicitly casting the return value. So the correct expression is “(NSUInteger)[indexPath row]==50”.
If you are having trouble figuring out what is in scope and what the correct syntax is set a breakpoint on the line you are interested in and then experiment at the gdb prompt.