How to integrate SwiftLint - iOS Apps ?

write clean code with style

Ashok Rawat
5 min readSep 29, 2022

SwiftLint is a swift code style analysis tool which helps flag stylistic errors in the code. SwiftLint enforces the style guide rules that are generally accepted by the Swift community. Developer can set their coding style rules and force them during development. It’ll show warnings and/or errors if the code violates the linting rules.

SwiftLint encourages to accomplish the following goals:

  1. Increased clarity of intent.
  2. Maintaining a higher level of code discipline.
  3. Increasing the code’s reliability.
  4. Produce quality code.
  5. Increase rigour, and decrease the likelihood of programmer error.

Reason Developer/Project team use SwiftLint

  1. Consistency:- Swiftlint provide consistency of patterns, indenting, spacing, style, and naming. When code has these things, It is easier to work with and can be developed faster with greater confidence. Inconsistent code prevents your peers from creating the appropriate structure required to support and extend a codebase.
  2. Readability:- Readability is writing and presenting code in such a manner that it can be easily read and understood by team members including the new members. SwiftLint checks patterns, indenting, spacing, style, and naming if any rule violet it produces errors/warnings and you can focus on the core functionality.
  3. Quality code:- It is useful when working in a team because warnings & errors are produced when the code is not up to standard. SwiftLint analyzers evaluate the quality of a codebase and make warnings and errors if the code violates the linting rules.

Installation of SwiftLint

There are many methods to install SwiftLint. Few are as follows.

Using Homebrew:

/ usr / bin / ruby ​​-e "$ (curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"brew install swiftlint

Using CocoaPods:

Simply you need to add the following line to your Podfile.

pod 'SwiftLint'

This will download the SwiftLint binaries and dependencies in Pods/ during your next pod install execution from the terminal. This is the recommended way to install a specific version of SwiftLint in Xcode since it supports installing a pinned version rather than simply the latest (which is the case with Homebrew)

you can find more installation methods here.

Usages in Xcode

To display warnings and errors in the issue navigator after SwiftLint integrates into the Xcode project we need to add a shell script in the run script phase with the following steps.

  • Select the project in the browser.
  • Select the target.
  • Select the build phases tab.
  • Tap more (+).
  • Choose New Run Script Phase and paste the below script.
  • Run and build the application.
export PATH="$PATH:/opt/homebrew/bin"
if which swiftlint > /dev/null; then
swiftlint
else
echo "warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint"
fi

After the script is added into the build phase, SwiftLint rules are checked at every build of the application.

Types of errors in Xcode

SwiftLint uses the two types of Xcode errors.

  • Warnings, which do not prevent the launch of the app.
  • Red errors, prevent the launch of the app and show you the build failed message.

If you go to the Issue Navigator, you will find there are multiple options in the bottom. You will find all the SwiftLint errors are on the build-time (compilation) errors.

Disable and Configure rules in code

SwiftLint can be configured by adding a .swiftlint.yml to the root of your repo. This file sits in the project’s main path. We can enable, disable or write custom rules in this YML file. You’ll need to add a .swiftlint.yml config file to your project if it doesn’t have one already.

1) Change the rule settings
For example, if a line is too long and exceeds 120 characters, we’ll have a ‘warning.’ But if it exceeds 200 characters, it’s considered unnecessarily long and we get a ‘red error.’

#type name
type_name:
min_length: 2
max_length:
warning: 50
error: 100
allowed_symbols: "_"

#identifier name
identifier_name:
min_length: 2
max_length:
warning: 120
error: 200
allowed_symbols: "_"

2) Disable the rule in the project
SwiftLint provides a default set of rules and you can also disable any specific rule. You can find more rules here.

disabled_rules: # rule identifiers to exclude from running
- trailing_whitespace
- statement_position
- unused_optional_binding
- trailing_whitespace

3) Exclude the code during linting
If you want to exclude specific code like third-party library/pod files you can also do that in .swiftlint.yml file.

excluded: # paths to ignore during linting. Takes precedence over `included`.
- Pods
- SwiftLint/Common/3rdPartyLib

4) Disable the rule in a specific file or line
To disable any rule, you can use the following comment. After this comment, that rule becomes disabled for the entire file.
// swiftlint:disable rule_identifier

If you want to disable the rule only in the next line of code using the below comment:
// swiftlint:disable:next rule_identifier

You can also disable the rule in the current line and previous line with swiftlint:disable:this and swiftlint:disable:previous respectively.

Below is an example of a .swiftlint.yml configuration file based on the official SwitLint documentation.

.swiftlint.yml

Autocorrect & Auto-fix SwiftLint errors

If you wish to fix violations as well, you need to add swiftlint --fix && swiftlint in the script instead of just swiftlint and run/build the application. This will mean that all correctable violations are fixed while ensuring warnings show up in your project for the remaining violations.

Make sure to have backups of these files before running swiftlint --fix, otherwise important data, files, code may be lost.

Conclusion

SwiftLint is open-source, which enforces developers to have code styles and guidelines. It is more straightforward to set up and free to use in swift projects. It is a great tool for those who want to write better & easy to maintain code. It also helps new team members to learn and adapt to coding standards.

Thanks for reading, you can follow me on Medium for updated articles.

If you have any comments, questions, or recommendations feel free to post them in the comment section below! 👇 and please share and give claps 👏👏 if you liked this post.

--

--

Ashok Rawat

Mobile Engineer iOS | Swift | Objective-C | Python | React | Unity3D