April 6, 2009
Debugging with Xcode
This handout has many authors including
Eric Roberts, Julie Zelenski,
Stacey Doerr, Justin Manis, Justin Santamaria, and Jason Auerbach.
Because debugging is a difficult but nonetheless critical task, it is important to learn the
tricks of the trade.
The most important of these tricks is to get the computer to show you
what it’s doing, which is the key to debugging.
The computer, after all, is there in front
You can watch it work.
You can’t ask the computer why it isn’t working, but
you can have it show you its work as it goes.
Modern programming environments
usually come equipped with a
which is a special facility for monitoring a
program as it runs.
By using the Xcode debugger, for example, you can step through the
operation of your program and watch it work.
Using the debugger helps you build up a
good sense of what your program is doing, and often points the way to the mistake.
Using the Xcode debugger
The Xcode debugger is a complicated environment, but with a little patience, you should
be able to learn it to the point where you code more efficiently and productively.
menu, there are two menu items for executing your code: the
option and the
So far you’ve probably been using
, which sets
up and runs your program on its own until it completes successfully or halts abnormally
because of a fatal error.
option, on the other hand, will set up and run your
program in the context of the debugger.
The debugger gives you the ability to stop your
program mid-stream, poke around and examine the values of variables, and investigate
the aftermath after a fatal error to understand what happened.
When you choose the
menu item, it sets up your program and then brings up the debugger window
without starting program execution.
At this point, you control the execution of the
program manually using the buttons on the debugger window.
You can choose to step
through the code line-by-line, run until you get to certain points, and so on.
The toolbar icons you should become familiar with are
These icons and their
corresponding commands are detailed in Figure 1.
Whenever the program has not yet been started, the
command will start
If it has started but has stopped,
will start it again from where it left off.
button is useful if the program is in an infinite loop or if you want to stop the
program manually to use the debugger.
command is useful if you want
to want to quit the debugger and return to editing before the program has finished
When a program starts with debugging enabled, the debugger window opens.