SwiftLint

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. SwiftLint is based on guidelines from the Swift style guide. To put it simply, it can help us with:

Installation

You can easily install SwiftLint using Homebrew: $ brew install swiftlint.

If you have already installed SwiftLint, you can update it to the latest version using the $ brew upgrade swiftlint command.

NOTE: Please update Homebrew to the latest version before installing and updating SwiftLint: $ brew update.

Running in Xcode

If you want to integrate SwiftLint to Xcode, add the following script to your target:

if which swiftlint >/dev/null; then
    swiftlint
else
    echo "warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint"
fi

Example

iOS SwiftLint Run script

And that's it. Now SwiftLint will run with default settings.

Configuration file

You can find all rules by running $ swiftlint rules.

You have the possibility to control which rule is disabled/enabled and set thresholds for warnings and errors for a given rule. Just create a .swiflint.yml file and add it to your project root directory.

To disable a rule, add the following to your .yml file:

disabled_rules: # rule identifiers to exclude from running
  - force_cast
  - force_unwrappingitespace
  - type_name

To customize a rule threshold for warnings and errors, you can add one of the following:

force_unwrapping: warning

line_length: 510

type_body_length:
  - 300 # warning
  - 400 # error

file_length:
  warning: 500
  error: 1200

You can also exclude some parts of your project, such as Pods, so SwiftLint won't check them:

excluded: # paths to ignore during linting. Takes precedence over `included`.
- Carthage
- Pods

Disable a rule in code

Rules can be disabled with a comment inside a source file with the following format:

// swiftlint:disable <rule>

The rule will be disabled until the end of the file or until the linter sees a matching enable comment:

// swiftlint:enable <rule>

For example:

// swiftlint:disable colon
let noWarning :String = "" // No warning about colons immediately after variable names!
// swiftlint:enable colon
let hasWarning :String = "" // Warning generated about colons immediately after variable names

It's also possible to modify a disable or enable command by appending :previous, :this, or :next to apply the command only to the previous, this (current), or next line.

For example:

// swiftlint:disable:next force_cast
let noWarning = NSNumber() as! Int
let hasWarning = NSNumber() as! Int
let noWarning2 = NSNumber() as! Int // swiftlint:disable:this force_cast
let noWarning3 = NSNumber() as! Int
// swiftlint:disable:previous force_cast

NOTE: Do not overuse this!

iOS SwiftLint rules

In accordance with our Swift Style Guide and our rules discussion, the configured file can be downloaded here: SwiftLint configuration.

Xcode trailing whitespace

By default, Xcode won't remove trailing whitespace if the line is empty. SwiftLint does not like that, and you will probably receive some warnings for this. For future coding, you should change Xcode settings and set it to automatically remove all trailing whitespace even if the line is empty.

iOS trailing whitespace

NOTE: This will not automatically fix the SwiftLint warnings you've already received, but it will prevent them from repeating.

That's it. Install SwiftLint, add the configuration file to your root directory, and lint up your project!