Apply Swift Style and Conventions with SwiftLint

Batuhan Gobekli
4 min readMar 5, 2021
Photo by Bundo Kim on Unsplash

What is SwiftLint?

SwiftLint is a tool to enforce Swift style and conventions. Keeping the codebase clean and compliant is a really difficult task, especially when working as a team. Every developer has their own rules and conventions when writing code. Because of that, we need tools to continuously maintain and fix codebase style and conventions based on GitHub’s Swift Style Guide.

In this tutorial, I will show the installation and usage of SwiftLint on Xcode.

1-Installation

You can either install SwiftLint to your iOS Project as Cocoapod framework or install your mac locally. It’s your choice.

Using Homebrew:

Simply open terminal and copy:

brew install swiftlint

If you didn’t install Homebrew open terminal and copy.

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Using CocoaPods:

Simply add the following line to your Podfile:

pod 'SwiftLint'

2-Integrate to Xcode

Go to application target.On left upper tab click “Build Phases” and click the plus sign and lastly click “New Run Script Phase”.

On the blank script area copy and paste the installation script below.

Copy script to the area and save after.Be sure check the checkboxes “Based on dependency analysis” and “Show environment variables in build log”

Note : Because we added script to build phase now whenever xcode starts to build the project this script will run.

Let’s go for a test run

  1. Let’s hit command + b for build our application
  2. After the build finished let’s take a look for logs.

Your will see a bunch of logs for our script.

  1. On first log our script did locate SwiftLint.It’s great 😊
  2. And printed out the version of it.
  3. But throw fatal exception about didn’t find any git repository.

It’s normal for now because I created my project locally and and didn’t add to local git repository. Lets fix that.

Note : You may not get this error because your project is already in git repository.

To fix basically go to Xcode and on top menu navigate to
Source Control → New Git Repository

After window will appear and select the project you want to add and hit “Create”.

Now you are good to go. Let’s build the project again and test if our script is working out.

Yaaay ! 👌

3-Testing SwiftLint

Let’s open up ViewController.swift file and try for default rules that pre-installed in SwiftLint.Write down force casting example to try.

You can find an updated list of rules and more information about them here.

You should gettting red and yellow warnings on the code. 😱

Warnings and Errors
trailing_whitespace → Lines should not have trailing whitespace.
force_cast → Force casts should be avoided.

Conclusion

Now SwiftLint script run after every build and lint for default rules. Now you are ready to apply Swift style and conventions for your project. 🎉

Bonus

For custom rules you at your project folder find out the swiftlint.yml file and start writing down your rules.

Useful Links :

Airbnb Swift Style Guide

Google Swift Style Guide

Raywenderlich Swift Style Guide

--

--