Jeenal Infotech

SwiftLint

For the developers, only writing a code is not only the important task, but they have to strictly follow the style guide line for the particular language. If you are working in a team, it’s vital to follow the style guide line for maintainable code.

Lint tools help to enforce that style guide line. SwiftLint is one of the lint tool that is based on Swift Style Guide.

Installation:

Using Homebrew

Type following command in terminal to install SwiftLint package.

brew install swiftlint

Usage:

To Integrate SwiftLint into Xcode Project, add 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

Run script phase for SwiftLint

 

Note that, above code will throw a warning if SwiftLint is not installed on the system. If you want all your team member will install it you can make build failed using following script:

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

That’s It. Now build your project (⌘B). SwiftLint will automatically identify warnings and errors in your code that does not comply with the default style guide line.

As of now we have successfully integrated SwiftLint in our project. But, If you will compile your project, you ended up with lots of warnings and errors. Even Xcode’s “Single View” project  template doesn’t pass SwiftLint’s default validation 😳.

Auto correct:

Some of the basic issues (such as whitespace at the end of a line) can be automatically fixed by SwiftLint. Just open the terminal and type the following command in your project root directory.

$ cd path/to/project/root/directory
$ swiftlint autocorrect
$ swiftlint autocorrect --path your_file.swift #For SwiftLint's auto correct on single file

Configuration of SwiftLint’s behaviour:

Now we have to fix all other issues that SwiftLint’s autocorrect was not able to do. But fixing all the issues one time it’s too much complex and time consuming. Fortunately it provides configuration options to enable/disable its pre defined rules. So, Initially I will disable all the rules and enable one by one to break down the bigger task into smaller chunk.

To get started, just create .swiftlint.yml in your project’s root directory and paste following contents.

disabled_rules:
    - trailing_newline
    - opening_brace
    - empty_count
    - comma
    - colon
    - force_cast
    - type_name
    - variable_name_min_length
    - trailing_semicolon
    - force_try
    - function_body_length
    - nesting
    - variable_name
    - conditional_binding_cascade
    - variable_name_max_length
    - operator_whitespace
    - control_statement
    - legacy_constant
    - line_length
    - return_arrow_whitespace
    - trailing_whitespace
    - closing_brace
    - statement_position
    - type_body_length
    - todo
    - legacy_constructor
    - valid_docs
    - missing_docs
    - file_length
    - leading_whitespace

Some rules have parameters that you can configure, for example the Type Body Length, how many lines of code the body of a type is allowed to be, can be configured like this:

type_body_length:
  - 300 # warning
  - 400 # error

Last but not least:

  1. To list all available SwiftLint’s rules type following command,
$ swiftlint rules
  1. All available rules which you can use are here.
  2. You can download the example configuration file from here.
Share On Facebook
Share On Twitter
Share On Google Plus
Share On Linkedin
Contact us

One thought on “SwiftLint

  1. see this

    Having read this I believed it was really enlightening. I appreciate you taking the time and effort to put this informative article together. I once again find myself personally spending way too much time both reading and leaving comments. But so what, it was still worth it!

Leave a Reply

Your email address will not be published. Required fields are marked *

Show Buttons
Hide Buttons