Checking Swift coding style and conventions using Swiftlint
SwiftLint is a tool which enforce to follow Swift style and conventions, based on GitHub’s Swift Style Guide.
SwiftLint checks the source code for programmatic as well as stylistic errors. This is most helpful in identifying some common and uncommon mistakes that are made during coding. Simply it can just helps us with:
- maintaining a higher level of code discipline
- increasing the reliability of the code
Installation
Using Homebrew
We can also install SwiftLint by downloading SwiftLint.pkg
from the latest GitHub release and running it.
Then integrate Swiftlint with Xcode by adding a new “Run Script Phase” with:
Now when we build our project, SwiftLint will let us know via regular Errors and Warnings in Xcode when there’s something to fix.
We can configure how SwiftLint behaves in complete detail by creating a new file called .swiftlint.yml
and putting it in the root directory of our project. We can fill out this file to customize (for example) which conventions are enforced:
We can disable rules “in-line” in our code with special comments:
References