Integrate SwiftLint into your project-Swift

Lucky Mehndiratta
3 min readJul 29, 2022

SwiftLint helps you to follow the style guide rules that are accepted by the Swift community. Follow the following steps to integrate the SwiftLint in your project when you are going to start your project so that you can do it from the starting point. But if you missed that no worries, Integrate it now and take care next time. This will help you more during your development.

First, add the pod in your pod file(If you are going to use pods the first time please create a pod file for your project, and then you can move further).
pod ‘SwiftLint’

After this, You need to save and run the command terminal on the project pod file path.
pod install

Once you are done with that, Now you need to create a swiftlint.yml file, For this again you need to run the command on the same path where you run the pod install.
touch .swiftlint.yml
This command will create a file and this file will be hidden on the same path if you want to check it. Go to the project folder path as you follow on the terminal and press shift+command+. (shift+command+dot)
The hidden files you can check there.

Now, Run the following command on the terminal to open the swiftlint.yml file so that we can add our rule for SwiftLint.
open .swiftlint.yml

Once you run this command swiftlint.yml file will open and you can add your rules to this file and save it.
open .swiftlint.yml

# ******* Add Rules as per your company Guidelines *******

disabled_rules: # rule identifiers to exclude from running
- colon
- comma
- control_statement
- type_name
- Variable_name_length
- identifier_name

opt_in_rules: # some rules are only opt-in
- empty_count
- empty_string

# Find all the available rules by running:
# swiftlint rules

included: # paths to include during linting. ` — path` is ignored if present.
# — Source

excluded: # paths to ignore during linting. Takes precedence over `included`.
- Carthage
- Pods
- Source/ExcludedFolder
- Source/ExcludedFile.swift
- Source/*/ExcludedFile.swift # Exclude files with a wildcard

analyzer_rules: # Rules run by `swiftlint analyze` (experimental)
- explicit_self
# configurable rules can be customized from this configuration file
# binary rules can set their severity level

force_cast: error # implicitly
force_try:

severity: warning # explicitly
# rules that have both warning and error levels, can set just the warning level
# implicitly

line_length:
warning: 300
error: 500
ignores_function_declarations: true
ignores_comments: true
ignores_urls: true
function_body_length:
warning: 30
error: 50

function_parameter_count:
warning: 6
error: 8

# they can set both implicitly with an array
type_body_length:
warning: 300 # warning
error: 500 # error

# or they can set both explicitly
file_length:
warning: 500
error: 600
ignore_comment_only_lines: true

cyclomatic_complexity:
warning: 10
error: 15

# naming rules can set warnings/errors for min_length and max_length
# additionally they can set excluded names

type_name:
min_length: 3 # only warning
max_length: # warning and error
warning: 40
error: 60

excluded: iPhone # excluded via string

#allowed_symbols: [“_”] # these are allowed in type names
identifier_name:
min_length: 2
max_length:
warning: 90
error: 1000

excluded:
#here you can add a file, or folder you want to exclude for this toll

reporter: “Xcode” # reporter type (xcode, json, csv, checkstyle, junit, html, emoji, sonarqube, markdown)

You can add or remove the rule from above or from the swiftlint.yml file as per your company/project guidelines.

Lastly, You need to add the script to your target, Go to your target, select build phase and click on the + icon. Select New Run Script Phase and give the name as per your choice and add the following command there.
${PODS_ROOT}/SwiftLint/swiftlint

Compile your code you will see the warning and do whatever needs to be done for this warning.

Useful Link:
https://github.com/realm/SwiftLint
https://github.com/raywenderlich/swift-style-guide

Happy Coding!!!

--

--