What is SwiftLint?
SwiftLint is a tool to enforce Swift style and conventions. Keeping the codebase clean and compliant is a really difficult task, especially when working as a team. Every developer has their own rules and conventions when writing code. Because of that, we need tools to continuously maintain and fix codebase style and conventions based on GitHub’s Swift Style Guide.
In this tutorial, I will show the installation and usage of SwiftLint on Xcode.
You can either install SwiftLint to your iOS Project as Cocoapod framework or install your mac locally. It’s your choice.
Simply open terminal and copy:
brew install swiftlint
If you didn’t install Homebrew open terminal and copy.
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Simply add the following line to your Podfile:
2-Integrate to Xcode
Go to application target.On left upper tab click “Build Phases” and click the plus sign and lastly click “New Run Script Phase”.
On the blank script area copy and paste the installation script below.
Copy script to the area and save after.Be sure check the checkboxes “Based on dependency analysis” and “Show environment variables in build log”
Note : Because we added script to build phase now whenever xcode starts to build the project this script will run.
Let’s go for a test run
- Let’s hit command + b for build our application
- After the build finished let’s take a look for logs.
Your will see a bunch of logs for our script.
- On first log our script did locate SwiftLint.It’s great 😊
- And printed out the version of it.
- But throw fatal exception about didn’t find any git repository.
It’s normal for now because I created my project locally and and didn’t add to local git repository. Lets fix that.
Note : You may not get this error because your project is already in git repository.
To fix basically go to Xcode and on top menu navigate to
Source Control → New Git Repository
After window will appear and select the project you want to add and hit “Create”.
Now you are good to go. Let’s build the project again and test if our script is working out.
Yaaay ! 👌
Let’s open up ViewController.swift file and try for default rules that pre-installed in SwiftLint.Write down force casting example to try.
You can find an updated list of rules and more information about them here.
You should gettting red and yellow warnings on the code. 😱
Warnings and Errors
trailing_whitespace → Lines should not have trailing whitespace.
force_cast → Force casts should be avoided.
Now SwiftLint script run after every build and lint for default rules. Now you are ready to apply Swift style and conventions for your project. 🎉
For custom rules you at your project folder find out the swiftlint.yml file and start writing down your rules.
Useful Links :
Check Out My Other Articles
Preparing Networking Layer Using Alamofire iOS
The summarized, common, organized way of networking
Scheduling Local Notifications (Swift 5)
Difference between Local and Push Notification