Integrating SwiftLint with Xcode using Pod

Mano
3 min readSep 28, 2021

Introduction
Sometimes it is tough to maintain the consistent codebase within a small or large team of developers, there will be different conventions and styles will be used across all the developers, so it will be tough to examine/understand the code for new developers or even to some experienced developers.

To avoid this difficult and to keep our codebase clean and neat SWIFTLINT tool is very useful.

1. Installation

To start with the installation, add the following code in your pod file.

pod ‘SwiftLint’

2. Integrating SwiftLint with Xcode

In order to get warnings and errors in Xcode, add the below code in run Scripts (Project Target -> BuildPhases -> Run Script),

“${PODS_ROOT}/SwiftLint/swiftlint”

For Reference, check the below image

If you don’t find the Run Script option in build phases, refer the below image

3. Testing Your Project with SwiftLint

In order to test the SwiftLint Integration with Xcode build your project, it will show the list of warning and errors based on the pre-defined rules on SwiftLint.

Clear all the warning and errors. If you don’t want to change some codes, you can also reconfigure the rules based on your requirement.

To do so, you have to create a .swiftlint.yml file on your project and add the rules, here are some examples

included:
— (Your Project Path)/(FileName/FolderName))

excluded:
— Pods

line_length:
warning: 500 # warning
error: 750 # error

file_length:
warning: 500
error: 750

disabled_rules:
— colon
— comma
— force_cast
— force_try

opt_in_rules:
— empty_string

function_body_length:
warning: 250 # warning
error: 500 # error

type_body_length:
warning: 500 # warning
error: 750 # error

large_tuple:
warning: 4 # warning
error: 5 # error

cyclomatic_complexity: 20

After adding the rules save and build your project, you can able to see the warning and errors are formed based on your configuration you have mention on .swiftlint.yml file.

4. Additional Info

To view the more no of rules, use the below command in your terminal

./pods/swiftlint/swiftlint rules

And also you can refer here.

--

--