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.

Tweak the Optimization Level — navigate to Build Settings -> Optimization Level and make sure that Debug is set to None. Next, go to Build Settings -> User-Defined and add SWIFT_WHOLE_MODULE_OPTIMIZATION = YES . From my experience, this flag always tackles a big part of the compiling time.

Optimization Level in Build Settings

Remove dSYM file from Debug— dSYM(debug symbols file) is a file which takes debugging information and stores it in a dSYM Bundle. This file is generated each time you compile your project. However, you don't need this in Debug so change the value to DWARF, and leave it active only for the Release mode. You can locate Debug Information Format setting under Build Settings.

Remove dSYM file from Debug

Check your Build Active Architecture Only values — for all the non-release builds set Build Active Architecture Only to YES. This will prevent Xcode to compile files twice. You can locate Build Active Architecture Only setting under Build Settings.

Check your Build Active Architecture Only values

Empty your Derived Data — if you have any kind of problems with building the project, you should start from here. Go to File -> Project Settings and click the arrow next to the Derived Data path. The arrow will locate the folder and just hit delete on it. Don't worry, it will recreate next time you run the project. Also, don't forget to clean the project after deleting the data (CMD+Shift+K).

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

Add type annotations — you should always add type annotations to your variables. That way the compiler will already know the type of the variable, and will save time reading it. For the example, I will use array literals.

This:

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

Instead of:

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

Avoid Nil-Coalescing operator — another big improvement in compile time was noticed upon removing nil-coalescing operators. For example, working with an optional String.

if let name = string{

/* string has value */

}else{

/* string is nil*/

}

Instead of:

let name = string ?? ""

Avoid Ternary Conditional Operators — ternary conditional operators are also increasing the Swift compile time. I love using both the ternary and nil-coalescing operators, as they make my code shorter and cleaner. But, we have to keep avoiding them as much as we can and work with the if-else conditionals instead.

var letter = ""

if isFirst{

letter = "a"

}else{

letter = "b"

}

Instead of:

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

Don't use + to concatenate a string — Swift prefers string interpolation rather than using the "+" symbol. Believe it or not, this have saved me a lot of compile time while optimizing.

This:

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

Instead of:

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

Pre-Compute — Never do computations directly from the if-else conditions. It takes a LOT of time to finish, and also it doesn't knows it's type yet. Only this step adds 5+ seconds to the compile time. What you need to do is, create a variable together with the type annotation that we have mentioned above, and add the calculation. After that, pass the value to the if-else condition and do your comparison.

let number: Double = 60 * 60 if number == 3600{ }

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.