Watch Your Language!

Batikan Sosun
3 min readAug 14, 2019

--

Image Credit: Getty Images

In this article, I will explain how we can write clean and readable code with SwiftLint. Before starting this article for good understanding maybe you need to take a look at my “ What’s the Lint(er)?”

Let’s deep dive to SwiftLint.

SwiftLint, a tool to enforce Swift style and conventions, it’s great it follows Swift guidelines, it’s supported by the Realm development team and it’s open-source which means it’s get updated regularly by the Swift community.

Now, we will go step by step for installing swift tool.

Requirements:

  • Xcode 10.0 or later.
  • macOS 10.14.4 or later.
  • Homebrew or CocoaPods or Mint one of the ways.

Installation:

Using Homebrew

if you don’t have it installed you can follow the steps on their website.

Once installed, open terminal and run below code

Install SwiftLint via Homebrew

Using CocoaPods

Simply add the following line to your Podfile:

Install SwiftLint via CocoaPods

Using Mint

Install SwiftLint via Mint

Using a pre-built package

You can also install SwiftLint by downloading SwiftLint.pkg from the latest GitHub release and running it.

And then go to your project folder and open the project.

On the main target, we are going to add a new “Run Script Phase” from the “Build Phases”.

It should look like the following screenshot.

if you build your app after installation you can see some error. Don’t worry, it’s normal. Because we have not yet set the configure file.(These errors comes from other frameworks, third-party or others.) Simply, all you have to do is create a new document inside your project folder named “.swiftlint.yml”.

And then open the “.swiftlint.yml” file add below code.

Save the file and build again.

SwiftLint has some rules. If you wonder the rules or contribute that community you can use the following link:

Auto-correct

SwiftLint can automatically correct certain violations. Files on disk are overwritten with a corrected version.

Rule inclusion:

  • disabled_rules: Disable rules from the default enabled set.
  • opt_in_rules: Enable rules not from the default set.
  • whitelist_rules: Acts as a whitelist, only the rules specified in this list will be enabled. Can not be specified alongside disabled_rules or opt_in_rules.
  • analyzer_rules: This is an entirely separate list of rules that are only run by the analyze command. All analyzer rules are opt-in, so this is the only configurable rule list (there is no disabled/whitelist equivalent).

Defining Custom Rules

If you want to define custom rules, you can define regex like following.

SwiftLint is a awesome tool for clean, consistent and readable code. Definitely taste it.

If you have any criticism, question, or suggestion, feel free to post them in the comment section below!

Thanks for reading.

References:

--

--

Batikan Sosun
Batikan Sosun

Written by Batikan Sosun

Tweeting tips and tricks about #swift #xcode #apple Twitter @batikansosun Weekly Swift Blogging

No responses yet