Intive Blog

SwiftLint to the rescue

How many times has it happened to you that you were very comfortable coding with your own style, in your own way but, suddenly, your team grew from just you, a single developer becoming a team composed by five others?

Not everyone will be comfortable with the way that you organize your code and your style, because each of us has its own approach. Can you imagine a team of six developers coding without sharing any guidelines? It would be a nightmare codebase! And that’s the special situation when SwiftLint comes to the rescue.

First of all, some definitions

Before digging deeper, let’s go over a few concepts.

  • What is a linter/lint?

A linter – or lint – refers to tools that analyze source code to find flag programming or stylistic errors, bugs, and suspicious constructs.

  • What is SwiftLint?

SwiftLint is a tool to enforce Swift style and conventions, loosely based on GitHub’s Swift Style Guide. It hooks into Clang and SourceKit to use the AST representation of your source files looking for more accurate results.

Installation

You have many ways to install SwiftLint on your mac, but the most common and simple one is by using Brew:

You can check the documentation if you are not familiarized with Homebrew.

Xcode configuration

Now that we accomplished the installation, it’s necessary to configure our project to run SwiftLint. In order to allow Xcode to run swiftlint command, we have to create a New run script phase on the target’s build phases and paste the following code:

 

Now you can build ( + B) your project, and be prepared to handle both errors and warnings during compilation. Some errors or warnings, like the shown below.

Warning!

Let me tell you in advance, that in case you use any dependency manager, you will also have errors from the 3rd party libraries we integrated. And this is when you might think “Hell, I don’t need this. I can live without a linter”, but let me tell you: ‘don’t lose your faith’.

You can customize which rules you want to run and how. So let’s configure them.

First of all, you need to be at the same folder level of your Xcode project.

 

This command will open an empty configuration file for our SwiftLint rules. Note that I use vi for text editing but you can use whatever you want.

Now, you might be wondering what to put in this file. Let me help you with that, by just providing you with a reference file.

 

Let me explain you a bit about what you just have seen.

disabled_rules: this key is very self-explanatory, it excludes every rule in our code that we don’t want Xcode to evaluate.

opt_in_rules: SwiftLint has a set of default rules, that runs without configuring anything. But it also has some optional rules, that by the way, are very interesting, but you have to specify on this file that you want those rules to run on every build command.

excluded: this key excludes the specified folders, files, etc. But maybe the thing that you may want here is the dependency manager, just because almost all the dependencies aren’t aligned with SwiftLint rules.

– The other lines just configure particular rules. For example the force_cast one is supposed to trigger a warning instead of an error, or the identifier_name key that works for the min and max values.

Sometimes the next magic command will be your best friend. It will help you a lot to avoid repetitive fixing fool errors like trailing or sorting the imports.

 

About SwiftLint

SwiftLint is maintained and funded by Realm Inc. But its code is open source, you can contribute to the project with new rules or bug fixing if you want. You can learn more about SwiftLint by taking a look at this video.

SwiftLint and CI integration

SwiftLint has an integration with Fastlane, another great tool for iOS development and for automating all your deploy process. But if you have a CI server and no Fastlane configured, you can also integrate SwiftLin if your CI server happens to be Jenkins, Travis or Circle.

Conclusion

In my opinion, SwiftLint is a great tool for iOS development, that really helps us to keep our codebase very clean, maintainable and aligned with both our team and the Swift Community.

SwiftLint enforces us to avoid some bad practices like performing a force cast or force unwrapping. It also leads us to learn some language features like it happens in my case with the for where clause.

Triggering example:

Non triggering example:

But not everything is happiness, you will also have to lead its implementation with your team. Some people will be annoyed by having warnings with every single line they write. And that’s up to you. But let me tell you, implementing SwiftLint is worth.

Nahuel Zapata

Nahuel Zapata is an iOS developer at intive-FDV since August 2016. Student of Systems Engineering at Universidad Abierta Interamericana, Nahuel is passionate about Swift and everything related to this language and technology. Avid reader, he likes to spend his time reading about architectures, scalable and testable code, server-side apps with Swift and Machine Learning. With great conviction, militates veganism as a way of life.

Add comment