Ghostboard pixel

Xcode: Treat Warnings As Errors

Xcode: Treat Warnings As Errors

It’s important to pay attention to compiler warnings. But in reality many developers tend to ignore warnings. In Xcode there is a very helpful build option called “Treat Warnings as Errors” that will force you to pay attention to warnings. This feature has been available for Objective-C code for some time. With Xcode 8, this option is now available for Swift code as well.

Hint: This post is using Swift 3 and Xcode 8

You can find the “Treat Warnings as Errors” option in the “Build Settings”. For Objective-C code, you can enable this feature in the “Apple LLVM – Warning Policies” section:

LLVM warning policies

For Swift code, the corresponding section is called “Swift Compiler – Warning Policies”:

swift compiler warning policies

So if you turn this option on, you will for sure never miss a compiler warning again because the build will fail when a warning occurs.  There’s a tradeoff though: At development time it’s very natural to create warnings, that are just temporary. So imagine you are developing some sort of code and you have already created a variable that you are not using yet. Now you just want to check whether everything is working correctly so far and let Xcode build the project. It won’t compile though because the unused variable causes a compiler error:

compiler error

In my opinion it’s good practice to build a project very often and just to take small development steps. If you are doing too much at a time, it becomes difficult to figure out where exactly a bug or a problem is located.

How to deal with the error? You could either fix this issue or turn the “Treat warning as errors option” off. Both options cost some time and are not good approaches.

So we are in a dilemma now. On the one hand we want good code quality by using the “Treat warning as errors” option, but on the other hand we want a fast development process. Here’s the solution: You can set the option for each configuration separately. So you can set it just for your release configuration:

swift compiler warning policies release

By doing so, you can still compile at development time without errors, but it won’t be possible to ship the app because there will be errors when you compile with the release configuration.

This will automatically teach you to take more attention to warnings at development time and the overall code quality will improve.

References

Title Image: @ so47 / shutterstock.com