Our favourite features of Xcode 9
At WWDC in June 2017, Apple announced a major update of Xcode, the native iOS coding environment. Although it is still in beta, we immediately jumped onto using the new IDE and have been exploring the new features. Here are a few that we have come to rely on in our day-to-day code-writing, and how you can set them up for yourself.
Naming variables is one of the most difficult parts of programming. Inevitably on any project you will need to change the name of at least one of your variables, which may be referenced in several different files. For instance, problem we often face is mis-typed words which then carry through a project, eg “vairable”.
Previously to fix these issues with Swift in Xcode, you had to manually change every instance of incorrect name. Xcode would throw syntactical errors which acted as a guide for where you should look, but each name had to be retyped.
But not any more! With the new ‘rename’ feature of Xcode 9, you can rename variables throughout a file, or even your entire project. To do so, right-click on a variable name and select “Rename…” from the resulting menu.
This brings up an overview of all the places where that variable name has been used. When you change the name originally selected, all other instances of that text will also be changed.
Magic! Now there are no more excuses for badly named variables or typos in the code. The new refactoring functionality makes it quick and easy to update your naming and ensure that your code is easily read and understood.
The speedy build system
When creating a new iOS project, you will probably hit the ‘run’ button or shortcut in Xcode tens or hundreds of times per day. We want to see the affect that our code change has had on the resulting app. However, the process of building the app takes precious seconds and often gets hung up in ‘compiling Swift source files’ for far too long.
Xcode 9 introduces a new build system which is lightning fast in comparison. Switching over is simple, you just need to select ‘New Build System’ from ‘Workspace Settings’ within the File menu.
To demonstrate how fast it is we ran a test where we made the same change (adding and removing a file) to a simple project and timed it once using the default build system, and once with the new build system. On the old build system, the compile time stopped the clock at 27 seconds.
The initial build after changing systems is slow, as the compiler has re-build all tasks, however subsequent builds are astonishingly quick. In our test, the same change and build as previously now clocks in at 3 seconds, a significant reduction in build time.
Less time spent waiting for projects to compile means less random coffee breaks, but does mean more time to code! Overall, we’ll count that as a win 😉
Adding threads to analysis
If you’ve ever had to deal with main and background threads, you’ll know that making UI changes can be perilous. A UI change made from a background thread (eg. in a completion) can result in your whole app crashing. Resolving the issue is incredibly frustrating, as it is difficult to diagnose and debug.
To combat this issue, Xcode 9 introduces thread checking to its analysis tool. During debugging, it detects method calls made on the background thread which will break your app. This will automatically run when you build your app, however can also be specifically accessed with the analysis tool, found by holding down on the ‘run’ symbol.
Resulting errors are listed within the ‘Runtime’ tab, with hints as to how to resolve the issue.
The inclusion of the Main Thread Checker tool in Xcode has already helped us pick up a few errors which otherwise could have crashed the app. It’s another way that Xcode 9 is improving our working process and allowing us to code and ship faster. ‘Git blame’ for threading bugs, be gone!
Bonus feature: Wireless Building
Whilst we haven’t tried it yet, a swanky addition to Xcode 9 is ‘network debugging’: the ability to build an app to your own iPhone without it being plugged into your Mac. You need to have iOS 11 running on your phone and connect to the same WiFi network to access the feature. One less barrier to testing is always appreciated, and getting it set up is high on our list.