Apply Swift Style and Conventions with SwiftLint

Photo by Bundo Kim on Unsplash

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.

1-Installation

You can either install SwiftLint to your iOS Project as Cocoapod framework or install your mac locally. It’s your choice.

brew install swiftlint
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
pod 'SwiftLint'

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”.

Let’s go for a test run

  1. Let’s hit command + b for build our application
  2. After the build finished let’s take a look for logs.
  1. And printed out the version of it.
  2. But throw fatal exception about didn’t find any git repository.

3-Testing SwiftLint

Let’s open up ViewController.swift file and try for default rules that pre-installed in SwiftLint.Write down force casting example to try.

Conclusion

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. 🎉

Bonus

For custom rules you at your project folder find out the swiftlint.yml file and start writing down your rules.

Useful Links :

Airbnb Swift Style Guide