Plugins are a great way for us to extend Xcode, adding functionality that Apple didn't ship with it. We've covered Xcode plugins a bit here in the past, notably in Bite #147, where we learned about using Alcatraz to install plugins. Today we'll look at a new plugin called BuildTimeAnalyzer from Robert Gummesson. It can help uncover why are code is compiling slowly. Let's dive in.
We'll start by completing the steps in Bite #147 to install Alcatraz. (If we're going to be installing and trying out plugins Alcatraz is a good way to organize and manage everything).
Once installed, we'll open the Alcatraz Package Manager by going to Window > Package Manager.
We can search for "BuildTimeAnalyzer" in the search field to find the plugin and install it. Once installed, we'll restart Xcode and open our project.
We can open the analyzer window by selecting View > Build Time Analyzer from Xcode's menu.
Next, we'll need to add some compiler flags.
(Remember to add these to any other targets your app may depend to see compile times for those too).
We'll add -Xfrontend and -debug-time-function-bodies to the "Other Swift Flags" in our app's target's build settings.
After that, one last clean and build and we should start to see a list of how long each bit of our code is taking to compile. Neat!
We can click on each result to jump straight to the line of code that's causing the slow down. The results are often surprising!
For example: Adding Collection types together seems to bring the Swift compiler to a slow crawl.
In Bite #147 we looked at installing Xcode Plugins using the Alcatraz Package Manager. Today we'll look at a few plugins we can install that will make the experience of writing code in Xcodemuch better. Let's get started.
First up is Fuzzy Autocomplete. In Bite #146, we covered using Xcode's "Quick Open" dialog. It has this great "fuzzy matching" feature where (for example) we can type something like "upvc" and it will match UserProfileViewController.swift. This plugin brings that exact same functionality to Xcode's autocomplete:
Next, a simple one. Normally when we press ⌘← in Xcode, it jumps all the way to the very beginning of the line. The Xcode Beginning of Lineplugin alters this, and instead jumps to the first character of code. Neat!
The QuickJumpplugin is super fun. We can activate it with a keyboard shortcut, then it will display a bunch of characters, we type the one where we want our cursor to move and we're there!
Refactorator let us rename functions, vars, enums etc. in Swift!
Finally, with the KSImageNamed plugin, Xcode will autocomplete names of images, when calling functions like UIImage(named:):
All of the plugins mentioned here are available in Alcatraz.