Nothing is perfect

Swift has become one of the greatest modern programming languages, but as everything else, it has its own issues. When working on large-scale projects, you can notice an issue where Swift compile time is growing. And it's a real frustration and waste of time. I have witnessed compile times up to 5–6 mins, but there are examples that show it can grow a lot more than that. In those moments, Xcode reminds me of Android Studio in the earlier days. 🐢🐢🐢🐢

Wasting an hour or two daily in Swift compile time is a lot of unproductive time. That's why we must optimize Xcode and the way we code. I will show some of the things that decreased the Swift compile time dramatically, so you can try it out and tell me if they have helped you.

It's optimization time!

An Awesome Tool

Let's start by installing this awesome open source tool, called Build Time Analyzer for Xcode. Install this tool by following the instructions provided on the GitHub link, and then run your project. It will analyze your code, and tell you which parts of your code take the most time to compile. It will be much easier to optimize the code with this tool, so don't skip this step.

Build Time Analyzer for Xcode in action

Xcode improvements

Before going to the code, let's check some optimization tips that we can try on the IDE.

Optimization Level in Build Settings

Remove dSYM file from Debug

Check your Build Active Architecture Only values

Empty your Derived Data

These are the Xcode improvements that give best results in decreasing the Swift compile time. Now, let's see some coding practices that you need to avoid/improve in order to get a better compile time.

Recommended for you: 6 Tips For A Software Development Success Philosophy

Swift improvements

This:let array: [String] = ["a", "b", "c", "d", "e", "f", "g"]

Instead of:let array = ["a", "b", "c", "d", "e", "f", "g"]

if let name = string{/* string has value */}else{/* string is nil*/}

Instead of: let name = string ?? ""

var letter = ""if isFirst{letter = "a"}else{letter = "b"}

Instead of: let letter = isFirst ? "a" : "b"

This: let url = "https://google.com/\("path")/\("anotherpath")"

Instead of: let url = "https://google.com/" + "path/" + "anotherpath"

Instead of: if number == 60 * 60 {}

Conclusion

The Swift improvements might look to you as minor improvements, and you might not be sure that it can help for decreasing your compile time. But, if you can count a number of variables you declare without a type annotation or overusing of ternary/nil-coalescing operators you will get to a big number of variables and each carrying a different amount of delay. When you sum all the delays you will understand that your compile time is in xx minutes.

I hope that these improvements will bring you a low Swift compile time and that you will focus more of your time on productive tasks. If you liked my post, please don't forget to 💚 or share this post with your friends. Also, you can subscribe to my newsletter below and read more interesting Swift tutorials.

That’s it from this tutorial and if it helped you please 👏 or share this story so others like you can find it. Thank you for your attention! 🚀

Check out my latest project:

‎VIP Sports Bet Tips & Scores on the App Store_This app is only available on the App Store for iOS devices. Increase your profits by following our professional BET…_apple.co

Read more of my writing on Medium:

Introducing Clean Swift Architecture (VIP)_Forget MVC, now!_hackernoon.com

Your ultimate guide to the Google Maps SDK on iOS, using Swift 4_Many iOS apps use Google Maps. This is a very common feature, so I have decided to prepare an ultimate guide on the…_medium.freecodecamp.org

SWIFT — Custom UIView with XIB file_Custom UIView with XIB file is a very common practice in iOS Development. Custom UIView classes don’t contain XIB files…_medium.com

How to add Spotlight support to your iOS app_A Swift tutorial that will make your app available in Spotlight search_hackernoon.com

Core Data Relationships_Understanding One-to-One and One-To-Many relationships_hackernoon.com

Understanding Auto Layout in Xcode 9_All you need to know about Auto Layout_hackernoon.com

Subscribe to my Newsletter: