SwiftLint
SwiftLint is an app which helps facilitate automatically checking style in Swift. When set up properly with the Vokal style files, this app will give you warnings for various style inconsistencies.
SwiftLint is open source. Use is therefore strongly encouraged.
Note that the style files do not cover all of our code standards, since SwiftLint only covers certain formatting options. You and your code reviewer are still responsible for keeping an eye out for anything the app might have missed. It is, however, a great way to keep from getting "Hey, you missed a bracket on the wrong line" comments in code review.
Initial Setup
If nobody on your project has already set up SwiftLint, the steps to do so are pretty simple. Here are the steps:
Instructions
- Copy the Vokal SwiftLint yml file into the same folder as your
.xcodeproj
file. Since this file is coming from the Vokal template, you will need to change the instances of___PACKAGENAME___
with the name of the project. - Add an Xcode build phase named "SwiftLint Run Script" with the contents of the script:
"${PODS_ROOT}/SwiftLint/swiftlint"
. - Add
pod 'SwiftLint'
to thePodfile
. - Run
bundle exec pod install
. - Build the project and SwiftLint should now run on every build. You can check the Xcode build log messages to verify that files are being linted.
Ignoring Specific Lines
There are multiple ways you can go about ignoring lines with SwiftLint. For instance, you can specify the rule you want to ignore in balanced swiftlint:disable
and swiftlint:enable
comments. In the example below the force_cast
rule is being ignored:
//swiftlint:disable force_cast
//The code you want to ignore for the force_cast rule
let foo = NSNumber() as! Int
//swiftlint:enable force_cast
It's also possible to modify a disable
or enable
command by appending :previous
, :this
or :next
for only applying the command to the previous, this (current) or next line respectively.
let foo = NSNumber() as! Int
// swiftlint:disable:previous force_cast
// swiftlint:disable:next force_cast
let foo = NSNumber() as! Int
let foo = NSNumber() as! Int // swiftlint:disable:this force_cast
More documentation is available at the SwiftLint site.
Vokal Limitations
- The SwiftLint binary is added to the repo and will run as a build phase both locally and on the CI server.
- Remember: This only covers a subset of our code standards. You are still responsible for following any standards not covered by the style file.