How to Manage Breakpoints in Your iOS App Code

You use the debugger to pause execution of your program at any time and view the state of the running code in your iOS app. You won’t find much to see in the Debug area and Debug navigator unless your program is stopped at a breakpoint or paused (and not much at those points, either).

The debugger is more useful to you if you set breakpoints to stop at known points and then view the values of the variables in your source code. Given that fact, it’s probably time to show you how to set a breakpoint and explain what a breakpoint is.

A breakpoint is an instruction to the debugger to stop execution at a particular program instruction. By setting breakpoints at various methods in your program, you can step through its execution — at the instruction level — to see exactly what it’s doing. You can also examine the variables that the program is using. If you’re stymied by a logic error, setting breakpoints is a great way to break that logjam.

To set breakpoints, open a file in the Source editor and click in the Gutter — the column between the Navigator area and the Focus ribbon that is adjacent to the Editor area — next to the spot where you want execution to stop.

You can toggle the state (on or off) of all the breakpoints in your program at any time by clicking the Breakpoints button at the left of the Debug bar: it's the colored button that's the second from the left. Note that the button looks like the breakpoint that's set in the gutter next to UINavigationController.

To disable an individual breakpoint, click its icon in the gutter. To get rid of a breakpoint completely, simply drag it off to the side. You can also right-click (or Control-click) the breakpoint and choose Delete Breakpoint from the pop-up menu that appears.

You can add a breakpoint to the statement just before you check to see if the device is an iPad.

image0.jpg

The Breakpoint navigator lets you see all breakpoints at once; if you select a given breakpoint in the Breakpoint navigator, it displays in the Source editor (where you can also edit it).

You can set several options for each breakpoint by Control-clicking the breakpoint and choosing Edit Breakpoint from the shortcut menu that appears.

image1.jpg

Doing so opens the Edit Breakpoint window, where you can set the actions and options you want for breakpoints added in the Breakpoint editor. You can set a condition for a breakpoint, ignore it a set number of times before stopping, add an action, and automatically continue after evaluating actions.

image2.jpg

The Add Action button was selected. You can also set a condition that you want the breakpoint to be triggered only if the networkStatus isn't equal to notReachable. In this case, you have to specify

networkStatus != 0

This is because networkStatus is not a symbol the debugger has access to, but rather an enumerated type (a set of named values that behave as constants). If you examine the Reachability.h file, you’ll find

image3.jpg
typedef enum {   NotReachable = 0,   ReachableViaWiFi,   ReachableViaWWAN} NetworkStatus;

The “normal” condition, of course, would be to set the breakpoint to stop when the condition is something you don’t expect, like the networkStatus equal toNotReachable. But If you want to keep your Mac connected to your network (which connects the Simulator as well), set the breakpoint condition to be equal to Not Reachable so it would stop at the breakpoint every time (unless of the network unexpectedly goes down).

Set this breakpoint and run your program in Xcode. You’ll be stopped at the breakpoint.

As you can see, when the breakpoint is reached, the Debug area is displayed and the Debug navigator opened automatically. (You can change that response in the Behaviors tab of Xcode Preferences.) It stopped because the condition set (networkStatus != 0) evaluated YES.

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
Advertisement

Inside Dummies.com