Getting started with SwiftLint

Swiftlint Rules

There is nothing better than spending some time to sharpen the saw —and improve the way that I code my apps. By finding new ways to improve the code quality, or efficiency in which I write my code — it pays off big time in being more productive and heck it’s even a bit more fun.

For this week — it was a no brainer. I was chatting with my friend Jane yesterday- and telling her about SwiftLint and her reaction was… “this is awesome” and you should blog about it now.. so here it is.

So here we are….

SwiftLint is one of the new standard tools that I now spin up at the start of every project. This nifty tool is one of the few that will:

  1. Help you to write better code
  2. Make sure that your code is readable and understandable to others who need to use your code,
  3. and most importantly.. for those of you like me who are on a quest to get those Xcode warnings list down to zero — it encourages you to fix the code as you type and thus you have the weighty bonus of having your GitHub full of lovely compliant code.

What it does…

SwiftLint works by applying over 75 (and counting) rules which are roughly based on the Swift Style Guide.

The SwiftLint Rules

An examples of the rules that really helped me were:

  • Trailing_newLine — SwiftLint highlights and warns if you put too many whitespaces lines at the end of a class ( a bad habit I picked up- which is now happily cured) It shows up in Xcode as a regular warning when you try build.

The great thing about SwiftLint is that it’s super flexible. You have the ability to use

  • all the rules on your project
  • Some of the rules on your project
  • None of the rules on your project
  • Or even apply some rules to only some classes.

Heard enough ? Let’s try it.

How to use it

To install SwiftLint — you need to:

  1. Install it on your Mac via HomeBrew as my recommended choice: brew install swiftlint
  2. Add the following build script to your project:
if which swiftlint >/dev/null; then
swiftlint
else
echo "warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint"
fi
Image for post
How to install SwiftLint

Then start building with CMD+B.

3. This is the most depressing part of the whole journey, when you zero error project suddenly generates a massive amount of warnings and errors. Don’t lose faith — we will fix them soon.

Below is an example from a project I use regularly called SWIFTCSV ( a CSV Parser which I am working on porting to Swift 4) where I went from zero warnings and zero errors to ….

Image for post
A very depressing number of errors after adding SwiftLint

The great news is that SwiftLint has a superb command line tool- which can automatically tidy up some of the errors for you (eg trailing line spaces)

4. Open Terminal and go to your project folder by dragging the project folder onto the terminal icon in the dock bar.

5. Run `swiftlint rules` to see what rules you have applied to your project

Image for post
A list of the swiftlint rules that were generating errors and warnings.

6. Run ‘swiftlint autocorrect’ which will try and fix the most common errors that it can. For me — it fixed a whopping 17 files straight away.

Image for post
Test result after running Swift Lint

The rest of the warnings and errors took me a mere 10 minutes, and I could already see the benefit for working with other peoples code, and also making sure that I play nice by giving others better code to use.

Is the tool any good ?

The tool was written by Realm, who have a wonderful simple to use database tool which I encourage you to try out. The team behind Realm have a focus on producing great Dev tools and the quality of SwiftLint is a great showcase for this.

SwiftLint tools rocks — go try it out now at the SwiftLint Github project