Using an debugger is an critical skill for any developer and can really improve your development.
Here’s why you should use the debugger:
- It’s faster: You don’t need to restart/recompile. Just set a breakpoint and you can start inspecting your app right away.
- It does not change your code: no need to add NSLog to your code and no risk of inadvertently commit those debugging lines.
- More information: The debugger lets you inspect all variables in the breakpoint’s scope, but also the stack trace. This means you can trace the calling functions or methods (as well as their respective variables)
Fortunately, Xcode embeds an debugger (gdb). Xcode offers a super-easy interface for interact with the interface, showing the stack trace, variables, the console and controls.
Adding a breakpoint is easy as clicking on the left of your code. To move it, simply drag-and-drop it at its new place. To remove it, drag-and-drop it outside of the area.
You can also edit the breakpoint by right-clicking on it.
Edit lets you add actions (like print object) and specify not to stop on this breakpoint.
To run the app with the debugger, be sure to enable the breakpoints.
When it reaches a breakpoint (and given breakpoints is enabled and the breakpoint doesn’t have Automatically continue after evaluating option enabled), the debugger will suspend the execution. You can the inspect the stack trace.
And also variables in the current scope and well as a console to manually interact with the debugger.
The debugger controls let you resume the program, step over or into instructions.
In conclusion, the debugger is a super useful tool. If you are not already using it, I urge you to try it out. I’m sure you’ll get addicted and won’t be able to work without thereafter!