We all have struggled maintaining our Coding Standard and discussed it more often. SwiftLint helps us maintain the Coding Standard. This article would go around how you can maintain your coding standards with SwiftLint. Learn how SwiftLint can be integrated in your project and how to get the best out of it.
Understand SwiftLint and how to use it to maintain your Coding Standards.
Why do we need to follow the Coding Standards?
It is important to have a Coding Standard and actually follow it as it makes the code consistent and therefore easy to maintain. Because anyone can understand it and modify it easily at any time. This gives us the ability to work on any module and switch between them easily, making the Team dynamic and Agile.
What is SwiftLint?
SwiftLint is a tool to enforce Swift styles and conventions, keeping the codebase consistent and maintainable.
How to use SwiftLint?
SwiftLint can be installed globally with brew or you can integrate it for a single project as a pod. We will be adding it as part of the project using the pod.
Add the following to the
pod install command in the terminal and it will install SwiftLint.
Now to add this in the Xcode, open
Build Phases and add a
Run Script with following:
NOTE: Keep this as the top most phase to be executed so that you get all the errors before the project files start compiling. This reduces the time majorly.
Hit Run and the SwiftLint will be executed automatically.
An example of how it looks like when the standards does not match:
Following all the Coding standards which the Swift Community is following is sometimes very difficult and preferencial. Hence SwiftLint has given the choice of which rules to follow and which ones to ignore.
You need to create a configuration file in the project root directory with name .swiftlint.yml. In this file, you can add the desired configurations like this:
excluded: # paths to ignore during linting - Pods disabled_rules: # rule identifiers to exclude from running - trailing_whitespace
As you can see, the
excluded allows us to exclude files and folders which we do not want to modify such as
The rules which we do not wish to follow goes in the
The saviour : Autocorrect
Yes, we can autocorrect the code which does not follow the rules of SwiftLint!
SwiftLint has offered a command
autocorrect which modifies the code with necessary changes.
Go to root of the project and Run the following command in the Terminal:
NOTE: Please make sure to have backups of these files before running swiftlint autocorrect, otherwise important data may be lost.
This command has limitations, it does not correct all the errors! It only corrects the rules listed as correctable. You can check the list of rules with the following command:
The SwiftLint is easy to use during the development itself and helps us follow the coding standards. We need to create a process which forces everyone to check the code before merging to the main development branch.
If you want to learn how to use
SwiftUI in your existing app, then you can read it here!
or If you want to learn how to use and convert your
SwiftUI, then you can read it here!