Monday, July 29, 2019

Linting Swift Code with SwiftLint

When working within a team, it is generally a good practice to follow a common coding convention and style guide. SwiftLint is a nice tool that helps to enforce style rules and best practices on code structure which makes all code in a project to have a familiar style. This helps to easily grasp the app logic rather than trying to work out the code differences. It's really easy to get started with adding SwiftLint to an existing project. Install the library using brew.
λ brew install swiftlint
Add the following script in the run phase under Xcode Build Phases section
if which swiftlint >/dev/null; then
echo "warning: SwiftLint not installed, download from"
This will enable the default SwiftLint configuration to be used to lint the code base. We can have custom rules defined under .swiftlint.yml file placed inside the project root. A sample config file is given below.
- Carthage
- Pods
- SwiftLint/Common/3rdPartyLib

- trailing_whitespace
- identifier_name
- type_body_length
- large_tuple

- unneeded_parentheses_in_closure_argument

force_cast: warning

warning: 160

warning: 1000

warning: 100
error: 200

allowed_symbols: "_"

reporter: "xcode"
The project is very active and contributions are welcomed. I did fix a bug and raised a pull request which got merged in release v0.32.0.