How to Debug in Xcode
Knowing how to debug is an essential skill for a developer. Not only to identify and solve bugs but to check and optimize your app performance.
In this article, we will see what debugging tools Xcode provides and learn how to use them properly.
Why I should use the debugger?
Developers commonly add print
or NSLog
statements for debugging. This technique could work but this is not the correct way of fixing bugs.
Why? It adds junk code after the development, leaving your app with a noisy console that makes diagnosing subsequent bugs more difficult.
Instead, you must know how to use the Xcode Debugger.
With this tool we can diagnose an error, reproduce it and then narrow down its root cause by inspecting variables at key points in your code while the app is running with breakpoints.
Steps to solve a bug
Follow these steps to narrow down the cause of a bug and to reproduce it:
- Determine where you think the bug happens.
- Use a breakpoint to pause your app at before of that point.
- Inspect variables and confirm that they have the values you expect.
- Step through the code, watch variables and check where…