How to use SwiftLint with Xcode to enforce Swift style and conventions?

Vineet Choudhary
Jun 24, 2018 · 5 min read
Image for post
Image for post

Originally published on DeveloperInsider: How to use SwiftLint with Xcode to enforce Swift style and conventions?

SwiftLint is a tool to enforce Swift style and conventions. Keeping a codebase consistent and maintainable in a project with a team of developers sometimes may be very hard, different conventions and styles, plus different levels of experience with the language across developers may result in most of the times in an application very difficult to debug and mostly very hard to understand for new developer joining the team.

1. Installation

Simplest way to install SwiftLint is by downloading SwiftLint.pkg from the latest GitHub release and running it. During the installation you may see this alert -

Image for post
Image for post

In this case you need to grant an exception for a SwiftLint by clicking the “Open Anyway” button in the General pane of Security & Privacy preferences. This button is available for about an hour after you try to open the app. To open this pane, open System Preferences and click Security & Privacy.

Image for post
Image for post

Now you just need to follow the installation instructions. You can also install SwiftLint using Homebrew by running this command:

2. Integrate SwiftLint with Xcode

In order to integrate SwiftLint with Xcode project target to get warnings and errors displayed in the Xcode IDE, you just need to add a new “Run Script Phase” with following script -

If you don’t know how to add a “Run Script Phase” in your project target, here an image for your reference -

Image for post
Image for post

3. Test and Configure

In order to test the integration of SwiftLint with your Xcode project target, you just need to build your application and you should have a lot of errors and warnings, mostly if your project has pods installed.

Image for post
Image for post

Don’t worry, the cool thing is that we can configure what rules to check or not when using Swiftlint. To configure it, all you have to do is create a new file inside your project folder named .swiftlint.yml. Here the general .swiftlint.yml file, which I recommend you to use in your project.

Now, Save configuration file and build, you will see that the number of warnings and errors are now so much lower. I recommend you to go to the Swiftlint rules file in their repository and see all of them. Or you can run swiftlint rules command to see all the rules. Also, this will validate your custom .swiftlint.yml file if you run this command in your project directory where .swiftlint.yml file available.

4. Hide Pod Warnings

You can hide Pod warnings from your Xcode Project by adding inhibit_all_warnings! in your Podfile for all 3rd party dependencies or :inhibit_warnings => true for specific dependency.

Then you need to execute pod install.

5. Auto Correct

A cool thing about Swiftlint is that it also can automatically correct certain violations by running the following command on the terminal -

Also, Before using the autocorrection feature, Files on disk are overwritten with a corrected version. So, make sure to have backups of these files before running swiftlint autocorrect, otherwise some data may be lost.

6. Configuration Parameters

The following parameters can be configured:

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.

You can also use environment variables in your configuration file, by using ${SOME_VARIABLE} in a string.

Here the great presentation by JP Simard.

Check out AppBox. AppBox is tool which build and deploy Development, Ad-Hoc and In-house (Enterprise) iOS applications directly to the devices from your Dropbox account. AppBox is a opensource software available on github https://github.com/vineetchoudhary/AppBox-iOSAppsWirelessInstallation .

Image for post
Image for post

developerinsider

Developer Insider

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store