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 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
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
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
Step 3: Open the .yml file. For this, Type
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
To customize a rule threshold according to our need for warnings and errors, add one of the following:
- force_unwrapping: warning
- line_length: 600
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’.
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.
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".