How to Reduce Bugs with Xcode 4 - dummies

By Neal Goldstein, Dave Wilson

Although some iOS developers think that writing code is where they spend the vast majority of their time when they’re developing an app, debugging is actually right up there as a very close second.

Because debugging plays such a crucial role in writing workable code, remember two important points:

  • App developers should strive to write code with as few bugs as possible (duh!).

  • App developers need to know how to use the debugger so they can track down the inevitable bugs they do introduce into their code as efficiently as possible.

With the release of Xcode 4, Apple has made it easier to write code with fewer bugs, as well as use the debugger to track down bugs you do have.

Because the best defense is a good offense, start by learning about the tools that Xcode provides that help you to write less buggy code. Xcode has figured out that the best way to make sure your code has as few bugs as possible is by giving you the opportunity to fix the code as you write it.

Such opportunities come in the form of Xcode’s various compiler warnings. More specifically, by taking advantage of the Live Issues and Fix-it features, you’ll catch many of your errors before you even run your program, and fixing them will be easy. (Well, some of them, at least.)

Live Issues continuously evaluates your code in the background and alerts you to coding mistakes, and Fix-it will also offer to fix the problem for you. Unless you are crystal clear about what you’re doing, don’t run your app without first resolving any outstanding compiler warnings.

Of course, Live Issues and Fix-it are really only good at fixing syntax errors — they’re usually not much help in detecting logic errors or coding mistakes that cause runtime errors (such as dividing by zero). For those errors, you need to become facile at using the debugger — or, more precisely, the Debug area and the Debug navigator.