Integrate SwiftLint into the Swift project

Tram Ho


SwiftLint is a source code execution engine that follows the styles and rules of the Swift language.

It will integrate in Apple’s Source Kit framework to analyze the project’s source code and ensure that the syntax and style of the source code follow agreed conventions in the project.

Steps for SwiftLint integration

To integrate SwiftLint into the project, we need to perform the following steps:

  • Install SwiftLint
  • Integration with XCode
  • Integration process verification
  • Configure rules and rules

Step 1: Install SwiftLint

There are many ways to install SwiftLint

Install via Brew

brew install swiftlint

Install via CocoaPods

We will have to follow the following line into the Podfile pod 'SwiftLint' file pod 'SwiftLint' This will allow us to download the SwiftLint source code and packages when running the pod install command and when running the script $ {PODS_ROOT} / SwiftLint / swiftlint in the build. phase

Install via Mint

$ mint install realm/SwiftLint

Install using the pre-build package

We can download and install the pre-build package SwiftLint.pkg on Github. The following message may appear on the installation process.

We need to authorize the SwiftLint.pkg file by going to Menu> System Preferences …> Security & Privacy> General then click the Open Anyway button.

Integration with XCode

We will need to integrate with Xcode for SwiftLint to parse the code and display warnings and errors in the IDE when building code. We will add “Run Script Phase” with the following content:

Integration process verification

To check if the integration has been successful or not? We just need to build the project Product> Build (⌘B) , if we see many warnings, or errors related to the syntax and style of the code, it means the integration was successful.

Configure rules

The SwiftLint bundle includes more than 75 basic rules and we don’t always use these rules. So we will configure the rules that will be used in the project via the .swiftlint.yml file

We try to configure the following content:

With the above configuration, we will receive an alert when a line of code is over 200 characters long, and receive an error message when a line of code is more than 250 characters long. No warnings are displayed when force_cast and force_unwrapping

The rule configuration includes:

  • disabled_rules: list of unused rules
  • opt_in_rules: list of usage rules
  • analyzer_rules: rules for parsers

We can refer to the configuration file below for my project

Reference source

Share the news now

Source : Viblo