Jeenal Infotech


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.


Using Homebrew

Type following command in terminal to install SwiftLint package.

brew install swiftlint


To Integrate SwiftLint into Xcode Project, add a new “Run Script Phase” with:

if which swiftlint >/dev/null; then
  echo "warning: SwiftLint not installed, download from"

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
  echo "warning: SwiftLint not installed, download from"
  exit 1

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.

    - 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:

  - 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.

4 thoughts 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!

  2. borvestinkral

    I was curious if you ever thought of changing the page layout of your blog? Its very well written; I love what youve got to say. But maybe you could a little more in the way of content so people could connect with it better. Youve got an awful lot of text for only having 1 or 2 images. Maybe you could space it out better?

  3. his explanation

    I just want to tell you that I am beginner to blogging and certainly loved this blog site. Probably I’m going to bookmark your site . You certainly come with great writings. Kudos for sharing with us your blog site.

Leave a Reply

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

Show Buttons
Hide Buttons