SwiftLint is a tool which enforce to follow Swift style and conventions, based on GitHub’s Swift Style Guide.

SwiftLint checks the source code for programmatic as well as stylistic errors. This is most helpful in identifying some common and uncommon mistakes that are made during coding. Simply it can just helps us with:

  • maintaining a higher level of code discipline
  • increasing the reliability of the code

Installation

Using Homebrew

brew install swiftlint

We can also install SwiftLint by downloading SwiftLint.pkg from the latest GitHub release and running it.

Then integrate Swiftlint with Xcode by adding a new “Run Script Phase” with:

if which swiftlint >/dev/null; then
swiftlint
else
echo "warning: SwiftLint not installed, download from 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:

disabled_rules:
- colon
- control_statement

We can disable rules “in-line” in our code with special comments:

// swiftlint:disable colon
let noWarning :String = "" // No warning about colon placement
// swiftlint:enable colon
let yesWarning :String = "" // Warning generated

References