Debugging code written in Swift/Objective-C can be tricky sometimes, especially for someone who’s just starting to learn iOS programming. We’re listing a few techniques and tools for debugging code in Xcode, that any successful mobile app developer should be familiar with. The inspiration for this list was the work we’ve done building our beautiful iOS app templates.
Xcode is a powerful IDE, hiding amazing advanced debugging tools that can be overwhelming for a new iOS developer. We’re not describing in details each of these tricks and tips since this post is only meant to make you aware of the existence of these debugging features. Please feel free to ask in comments any questions you might have.
1. View Hierarchy Debugger
An extremely powerful tool that provides you with a way to visualize your views and the UI hierarchy. You can rotate the screen, select any view, check out children as well as the name of their view classes. You can also inspect the auto layout constraints or find the memory address of the objects underlying the views. For a detailed explanation of how to debug views in Xcode, check out this tutorial.
2. Advanced Breakpoints
Breakpoints can be used for actions much more complex than simply breaking into a method and pausing the execution at a specific line to inspect the current application context. Try editing a breakpoint and check out the wide variety of options. Xcode support conditional breakpoints, sets the number of times before stopping for each breakpoint as well as takes specific actions when an advanced breakpoint gets hit, such as logging, running a shell command, an Apple Script or even playing a sound. Extremely useful, especially when debugging multithreaded environments or methods that are being called tons of times (such as layoutSubviews, for example). To find out more, check out this guide for how to use breakpoints in Xcode.
3. Generic Breakpoints
Showcased in the picture below, you can use them to pause the execution of the application right before an exception is being thrown out or a test case fails. Extremely powerful debugging technique. Check out Apple’s documentation for each of them, to learn more details about these generic breakpoints.
4. Debug Navigator
The debug navigator in Xcode has a set of effective debugging tools. In addition to displaying how much memory, network, CPU and disk your current app instance is using, it has an interactive call stack navigator, listing all the threads and grand central dispatch queues currently running in your app’s sandbox, together with their current call stack. You can click on each method’s signature and inspect the object graph and app state context at each step of the execution. This is amazing for debugging code as well as ramp up on large codebases since it shows exactly where all the methods are being called from and in what order. Amazing!
5. Location Simulator
Next to the “Debug View Hierarchy” button, there’s a “Simulate location” feature, that will allow you to simulate any locations you want to test your app at. Greatly useful for ensuring your app behaves properly in any location.
6. Debugging Slow Animations
This is more of an iOS Simulator debugging trick, but it’s extremely effective for iOS apps when debugging animations. It does what the name implies: slows down the execution of the UI animations so that they can be observed with human eyes. In this way, you can notice any glitches in your animations and you’ll be able to find out when and why they happen easily.
7. Color Blended Layers
If you’re debugging layout performance for your iOS mobile app, you should turn on “Color Blended Layers” options on your simulator. This feature will highlight in a red color which areas of your UI layout takes a lot of time to render, and therefore affect performance. Things like blurring effects or unwise opacity settings can affect the performance of your app. Ideally, all the highlighted layers should be green on the entire screen.
8. Quick Look
This button will allow you to visualize the selected in-memory instances of your classes, when hitting a breakpoint. This can mean different things for different classes. For example, you can view the real image that’s being stored into an UIImage object (just like in the following photo). Check out Apple’s documentation for a full understanding of the Quick Look debugging tool.
9. Simulate Background Fetch
Found under Xcode -> Debug -> Simulator Background Fetch, this tool is very powerful when debugging iOS apps that make use of the background fetch API, making it possible to manually trigger an operation that’s otherwise being done by the operating systems, in a production environment.
10. LLDB Advanced Debugging
A complete command language, powering an excellent debugging experience for experienced developers. You can basically run complex commands directly on the objects that exist in the current app’s sandbox. Check out Apple’s documentation on LLDB. We’re highly recommend using Facebook’s Chisel, which is rich open source set of LLDB commands built by Facebook.
Did we miss your favorite Xcode tool that you’re using for debugging code in iOS apps? Tell us about it in the comments!