Debugging is an essential part of the app development process in Swift. IntelliJ IDEA provides a comprehensive debugger for Java code, which can also be used to debug code written in other languages with the help of installed plugins. During a debugging session, you launch your program with the debugger attached to it, allowing you to interfere with the program execution and gain insights into what's happening under the hood.
Before diving into the debugging process, make sure the Generate debugging info option is turned on (the default setting) in . This setting is not mandatory for debugging, but we recommend leaving it enabled to ensure seamless functionality. You can also configure common debugging properties and behavior in .
If you're new to debugging, the out-of-the-box configuration will work just fine. For advanced users looking for specific settings or properties, refer to the Debugger reference section.
When setting up a run/debug configuration, define your custom settings if needed. This is particularly useful when passing arguments to the program or performing special activities before launch. For more information on setting up run/debug configurations, check out the Run/debug configurations section.
General Debugging Procedure
There's no one-size-fits-all approach to debugging applications in Swift. The process often requires a combination of different actions in various orders, depending on your specific needs. This topic provides general guidelines for typical debugging steps, with detailed information on how and when to use particular features available in the respective topics.
To start debugging, define where you want the program to be stopped using breakpoints. Breakpoints are special markers that represent places and conditions when the debugger needs to step in and freeze the program state. When the program is frozen by the debugger, it's referred to as suspended.
Alternatively, you can manually suspend the program at an arbitrary moment, but this method has limitations on the debugger functionality and doesn't allow for much precision as to when to suspend the program.
Running Your Program
Run your Swift app in debug mode. This might be a regular application, a unit test, or any other executable code, as long as the corresponding run configuration supports debugging. You can also run multiple debugging sessions simultaneously, just like with regular running of the program.
After suspending the program, use the debugger to gain insights into the state of your program and how it changes during running. The debugger provides information about variable values, the current state of threads, breakdowns of objects currently in the heap, and more. You can also test your program in various conditions by throwing exceptions or running arbitrary code right in the middle of the program execution.
Stepping Through Your Code
The stepping feature gives you control over step-by-step execution of your Swift app. By combining this feature with the tools provided by the debugger, you can deduce where the bug is coming from and test your program for robustness.
When you've determined what needs to be fixed, you can make changes without terminating the session. IntelliJ IDEA provides a functionality allowing you to adjust and reload pieces of your code on the fly. This approach is covered in the Reload modified classes topic.
Debugger Essentials
If you're already familiar with the IntelliJ IDEA debugger and want to get an overview of various useful features and approaches, check out the Debugger Refresher video series. The Debugger Essentials topic covers basic topics like line breakpoints, stepping, controlling the debug session, watches, expression evaluation, and breakpoint conditions.
The Debugger Advanced topic covers more advanced features like breakpoint types and settings, advanced stepping, remote debugging, renderers, and more. For expert users, the Debugger Professional topic provides an in-depth look at advanced topics like using HotSwap, debug labels, asynchronous stack traces, and debugging decompiled code.
By mastering these debugging techniques, you'll be well-equipped to tackle even the most complex issues in your Swift app development projects.