Conditional debugging in Xcode makes life easier

Reading Time: 1 minutes

The troubles

If you’re a developer using Xcode on a regular basis, you probably have had one of those situations where your code works fine while running through an array or other collection object and it suddenly – for no known reason – crashes into the ground on the 156th item. Maybe invalid data, maybe a valid data type that isn’t parsed or handled correct. Who knows.

The solution

Gladly, you don’t have to run through the breakpoint that you’ve setup in that same function for 155 times until you get to the 156th item. Coding around to hold that item also seems madness.

As described in Apple’s documentation on Xcode Debugging Guide: Managing Program Execution you can set conditions for your breakpoints.

You can setup the condition field by going to the breakpoint overview. Navigate there by using the menu (Run->Show->Breakpoints) or by right-clicking on your breakpoint and choosing “Edit in breakpoints” or “Reveal in breakpoints”

You will get an overview and you can double-click the field behind your breakpoint, as indicated in the following image.

Conditional breakpoint field in Xcode

The conditional breakpoint field column in Xcode

If, for instance, you want to use the breakpoint for the situation that i mentioned earlier you will need to:

  • Click the condition field behind the corresponding breakpoint
  • Simply add a boolean statement, like – in my case -: x==156

It will then run through your breakpoint until the condition is met (after 155 times it will break). And there we are.

There’s more that one can do, like breakpoint actions (let Xcode say something out loud, run a script or whatever) but i’ll save that for a later time. In the meantime, checkout Apple’s docs about decoding: Xcode Debugging Guide: Managing Program Execution.

The conclusion

It will save you some time once you’ve experienced with it!