Improve Code Quality and Standard Using SwiftLint

Posted By : Satish Thakur | 16-Sep-2019

Improve Code quality and standard using SwiftLint

 

I am writing this blog just to through some light on "how to integrate or setup swiftlint linting tools with XCode??" and "how to improve the swift code standard and quality??"

 

Swiftlint

  • SwiftLint is an open-source tool which is used to enforce Swift style and conventions in writing code. It is developed by Realm.
  • We can set different rules as per our coding standards and styles during the development of new and existing projects. 
  • Swiftlint has a command-line tool and Xcode plugin which helps to fits our development environment. 
  • If we violate the linting rules, it will show us warnings or/and errors based on our pre describe rules.

 

SwiftLint Integration or setup:

Let's follow the steps to setup up swiftlint with your Xcode project:

 

A) Swiftlint Installation 

Step 1: Install the pod file

Simply add the following line to your Podfile

pod ‘SwiftLint’

This pod will download the swiftlint binaries and dependencies in Pods/ during our next pod install execution.

 

Step 2: Add and Run Build Script

Go to your build phase setting and add a new run script

“${POD_ROOT}/SwiftLint/swiftlint”

You are done with swiftlint basic setup with your Xcode project.

 

B) Swiftlint rules configuration with the project or add .swiftlint.yml file: 

Step 1: Open a terminal and go to your project directory.

 

Step 2: Add .yml file. For this, Type

touch .swiftlint.yml

 

Step 3: Open the .yml file. For this,  Type

open .swiftlint.yml

 

Step 4: Set the code rules and styles.

  • Rule inclusion:
  • disabled_rules: Disable the rules from the default enabled set.
  • opt_in_rules : Enable rules which are not from the default set.
  • whitelist_rules: Only the rules specified in this list will be enabled and it can not be specified along with disabled_rules and opt_in_rules.
  • analyzer_rules: it is a separate list of rules that are only run by the analyzer command. All analyzer rules are opt_in, so analyzer_rules is the only configurable rule list. There is no disabled or whitelist equivalent.

 

Step 5: To see all default enabled rules by realm for swift language, Type

$ swiftlint rules in your terminal.

 

Step 6: 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
  • Variable_name_length

 

To customize a rule threshold according to our need for warnings and errors, add one of the following:

  • force_unwrapping: warning
  • line_length: 600     
  • type_body_length:

300 #warning

400 # error

 

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

  • Excluded: # paths to ignore during linting. Take precedence over ‘included’.
  • Carthage
  • Pods

 

Point to remember:

Disabling any default rule or customizing it according to our need is not a good practice at all because by doing so we are violating the swiftlint coding rules and styles.

So try to avoid disabling the rules until it is very much necessary.

 

 

Thanks. 

About Author

Author Image
Satish Thakur

Satish is working as a Mobile Application Developer. He is eager to learn about technologies and never neglect the opportunity. He believes in "Don't only dream, Work for it".

Request for Proposal

Name is required

Comment is required

Sending message..