Installing and Using SwiftLint

Get rid of unreadable habits

Image by Art Lasovsky on Unsplash

Before we start

Who is this guide for?

You might say that the steps for this article are contained in the repo for SwiftLint: https://github.com/realm/SwiftLint, indeed they are.

What is SwiftLint for?

SwiftLint is for enforcing Swift coding style and conventions. This means that your code will be easier to read, understand and maintain. This does not make your code automatically (in inverted commas) ‘good’, but it helps enforce one of the components that makes code actually good.

The installation

Installing and running SwiftLint should (in my opinion) be global. This means you will be able to check for code quality for any file on your Mac. To do so we can use Homebrew, which is a good system to install the software that you need on your Mac to make things run rather well (as a developer at least).

Basic usage: The command line

If you have some code that isn’t great (for all sorts of reasons, but I’ve produced this as an example for force-unwrapping rather that an example of anything else, sorry).

let array: NSArray = NSArray(array: [5, 6, 7])let numbers = array as! [Int]print(numbers.reduce(0, +))
SwiftLint
Force Cast Violation: Force casts should be avoided. (force_cast)
ViewController.swift:14:29
swiftlint autocorrect

Basic usage: Xcode

In Xcode wouldn’t it be great if we could get a pointer to where the errors are? It is true that we can use Build Scripts in Swift to help us out. To do so, we click on the Project in the file navigator, then click the primary app target, and go to Build Phases then press + to add a run script. In the case of the included project the project is called SwiftLintInstallation — you can see the three steps that need to take place int he following screenshot:

if which swiftlint >/dev/null; thenswiftlintelseecho "warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint"fi
let array: NSArray = NSArray(array: [5, 6, 7])let numbers = array as? [Int]print(numbers?.reduce(0, +) as Any)

Disabling warnings

You can turn off warnings for lines with swiftlint!

// swiftlint:disable:next unused_optional_binding
// swiftlint:disable line_length
// swiftlint:disable:next line_length
// swiftlint:disable:this line_length
// swiftlint:disable:previous line_length

Disable Pods Warnings and Errors

You might be in the situation where you have Pods, and they have many warning in them.

excluded:- Pods

Conclusion

I’d recommend you take a look at the attached repo to have a look at how I’ve got this small it of code to work without any errors or warnings. I’d recommend that you don’t let any code go into production with any errors (of course you shouldn’t!), or any warnings at all.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store