Make your code prettier by setting up SwiftLint in your projects.
Implementing SwiftLint is a common and easy way to get started with CocoaPods and quality management in your projects. In this article I’ll go through how to set up SwiftLint in your project and what it actually does.
Linters are tools coders use to enforce coding standards and rules. Such rules can be: “No trailing whitespace”, “variables are written in_snake_case” and pretty much anything else you can think of to make you code consistent, while dodging some weird bugs in the process. (For an indie iOS dev like me, it’s also a great tool for keeping me from doing dirty hacks.)
SwiftLint is a great open-source tool for enforcing good coding standards on your Swift code and it’s very easy to get started. In this tutorial we will use CocoaPods, a very popular dependency management system for Swift and Objective-C projects.
If you don’t already have CocoaPods installed, you can install it by running sudo gem install cocoapods in your terminal.
If you are working with a project that doesn’t use dependency manager or are working with a new project, you can use a handy command pods init in your terminal to set everything up. This creates a Podfile in your directory. Type open Podfile in your terminal and it will open the Podfile for you. Now you just need to add SwiftLint into the Podfile by adding this line into the Podfile pod 'SwiftLint' . Pretty simple. Now your Podfile should look something like this:
# Uncomment the next line to define a global platform for your project
# platform :ios, '14.0'
target 'Nifty Combat Helper' do
# Comment the next line if you don't want to use dynamic frameworks
# Pods for Nifty Combat Helper
Then install the pods by runnin pod install . This downloads all the pods declared in your Podfile and generates a CocoaPods project for you. From now on, you should use the .xcworkspace file instead the old familiar .xcodeproj file.
Now navigate to your Build Phases menu in you project. (In Xcode 12 — in the sidebar, click on the .xcodeproj -file. That is the highest level file you should see in the sidebar, then click on Build Phases.)
Click on the little + sign and add a New Run Script Phase. In the Run Script phase add:
If you are not working with a new project, you will probably have a lot of warnings and errors popping up right now. So time to start fixing your code!
SwiftLint runs great out of the box with some great rules, but you can add a .swiftlint.yml -file to enable or disable rules. Or make custom ones. You can find all the rules and how to disable or enable them from the SwiftLint-repo README.md.
I disabled multiple_closures_with_trailing_closure and line_length rules by adding these lines to the .swiftlint.yml file:
Note: if you disable rules, make sure you know the reason why you don’t want to enforce those rules in your codebase and what problems that decision can bring in the future.
A extremely useful command to keep in mind is the autocorrect function that you can use if you installed SwiftLint using Homebrew. I personally don’t like to use it, because that makes me pay less attention to the mistakes I make writing my code, since I don’t have to correct them myself.
Hope this was helpful, now let’s go write some quality code!