Debugging code in Swift can be tricky at times. This is especially the case for someone who is beginning to learn iOS programming. While Apple’s Xcode IDE (integrated development environment) is incredibly robust for most iOS developers to use daily, it can also be complex. Packed with tons of tooling, each iteration of Apple’s Xcode means new tips, tricks, and shortcuts to remember.
What is Xcode?
Before we jump into the debugging tricks, let’s see what Xcode actually is. Xcode is an IDE that is an integrated development environment. It was created by Apple to develop software for macOS, iOS, watchOS, and tvOS. It is also the company’s only officially supported tool for developers to create and publishing apps to Apple’s app store. It has been designed to be use seamlessly by both beginners and experienced developers.
Xcode includes all the relevant tools such as a text editor, a compiler, and a build system. With Xcode, you can write, compile, and debug your app, submit it to the App Store once you are finished. With wide ranging tools, Xcode enables the development process to be easier and faster.
Advantages of using Xcode
By providing developers an Assistant Editor, Xcode IDE aids users to develop apps for Apple devices with tremendous ease. The editor shows the relevant resources that it has deemed useful in the coding process without hindering the development process. This feature enables developers to complete their projects faster.
Xcode’s Assistant Editor is a robust tool that developers can turn to for finding information about their project. The development workflow is not disrupted even when developers have to find specific data.
Developers can test their apps no matter what the targeted device is. This allows them to check the app’s quality and fix problem areas.
Here’s a list of few easy tricks that you can use while debugging code in Xcode.
1. Slow animation
Testing animations on Swift can be tricky. But fret not, you have Xcode to help you out. Xcode comes with a feature that slows down animations in the simulator. This way, you can see exactly how your views are moving or changing. Enabling this feature is also quite easy. When you run your app, click Debug in the top menu once in the simulator and then select Slow animations.
2. Debug navigator
Xcode’s debug navigator comes with a set of effective debugging tools. While displaying how much memory, network, CPU and disk your current app is utilizing, it also has an interactive call stack navigator. The navigator lists out all the threads and grand central dispatch queues that are running in your app’s sandbox. At each step of the execution process, you can open each method’s signature to inspect the app state context.
3. View hierarchy debugging
In UI debugging, it is helpful to examine your app’s view hierarchy. Xcode lets you see a 3D view of your app where it shows you your views and subviews, all arranged along the z-axis. Using this feature is also quite simple. You have to first run your app. At the bottom debug area, click Debug View Hierarchy.
4. Color blended layers
To debug the layout performance of your app, turn on the “Color Blended Layers” option on your simulator. Highlighting in red color, this feature points out the areas of your UI layout that takes more time to be rendered, consequently affecting the app performance. Features such as blurring effects or unwise opacity settings can affect the performance of your app. Ideally, the highlighted layers on the simulator should be green.
5. 3D View
Larger apps can be difficult to debug. At times, Xcode won’t provide any answers while reading through your view hierarchy, In such a scenario, you can run your app in 3D View. After running your app, get into the Debug menu and select View Debugging. After this, click on Capture View Hierarchy. Once Xcode loads your hierarchy, you can grab and drag your view around in it. Xcode will show all of your views in 3D.
6. Quick Look
Quick Look button will help you visualize the selected in-memory instances of your classes. For instance, you can view real images that has been stored into an UIImage object. This is just one of the many benefits of this feature.
7. Location Simulator
Next to the Debug View Hierarchy button, you will find the Location Stimulator feature. This will allow you to simulate any location where you want to test your app. This feature will ensure that your app functions seamlessly in any location.
Additional tricks to try
- If some part of your code is not working, you can quickly toggle the debugging window by pressing ⌘ + ⇧ + Y
- For quick addition of documentation, select part of code and press ⌥ + ⌘ + /
- To add multiple Auto Layout constraints in Interface Builder, hold down ⇧ key as you’re clicking.
- To access the filtering panel presented above, tap ⌥ + ⌘ + J
While finding and fixing bugs in your app is not exciting; with Xcode, it is definitely easier. Improve the overall user experience of your iOS with Xcode today!