---
title: "#149: Enforcing Coding Style and Conventions with SwiftLint 🚿"
url: https://littlebitesofcocoa.com/149-enforcing-coding-style-and-conventions-with-swiftlint
author: Jake Marsh
published: 2015-12-17
topics: ["Tools","Libraries","Swift"]
description: "Style is important when writing code. Following conventions and guidelines helps tell the story of our code to our team and our future-selves."
---

# #149: Enforcing Coding Style and Conventions with SwiftLint 🚿

By Jake Marsh, December 17, 2015. HTML: https://littlebitesofcocoa.com/149-enforcing-coding-style-and-conventions-with-swiftlint

Style is important when writing code. Following conventions and guidelines helps tell the story of our code to our team and our future-selves. Today we'll learn about [SwiftLint][1], a tool from [Realm][2] that can help us enforce Swift conventions and best practices. Let's get started.

_SwiftLint_ provides a downloadable install package on its [Github page][3], but here we'll install using [Homebrew][4]. We'll run:

``` shell
brew install swiftlint
```

Now, we can run `swiftlint rules` at the command line to see all the different convetions and standards that will be enforced. We can also run `swiftlint lint` in the root directory of our project to see what rules we're breaking right in the terminal.

Our next step is to add _SwiftLint_ as a build phase to our project.

We'll head over to our **project's settings**, then to the **Build Phases** tab. We'll click the **+ button** to add a** new “Run Script” phase**. We'll name it <i>“SwiftLint”</i> and give it the follow script content:

``` bash
if which swiftlint > /dev/null; then
  swiftlint
else
  echo “Download SwiftLint: https://github.com/realm/SwiftLint"
fi
```

Now when we build our **project**, _SwiftLint_ will let us know via regular **Errors** and **Warnings** in **Xcode** when there's something to fix.

We can configure how _SwiftLint_ behaves in complete detail by creating a new file called `.swiftlint.yml` and putting it in the root directory of our **project**. We can fill out this file to customize (for example) which conventions are enforced:

``` yaml
disabled_rules:
  - colon
  - control_statement
```

We can disable rules “in-line” in our code with special comments:

``` swift
// swiftlint:disable colon
let noWarning :String = "" // No warning about colon placement
// swiftlint:enable colon
let yesWarning :String = "" // Warning generated
```

Finally, _SwiftLint_ can correct some violations (trailing_newline, trailing_semicolon, etc.). Just run `swiftlint autocorrect`.

More info about _SwiftLint_ can be found at [git.io/swiftlint][5]

[1]: https://github.com/realm/SwiftLint
[2]: https://github.com/realm
[3]: https://github.com/realm/SwiftLint/releases/latest
[4]: http://brew.sh/
[5]: http://git.io/swiftlint
