On this part we’ll begin by putting in SwiftLint. Then we’ll configure it to inform it which guidelines to examine for. Subsequent we’ll configure Xcode to run SwiftLint through the construct part. Lastly we’ll discover ways to disable guidelines on items of code and even entire recordsdata.
Listed below are the steps:
- Set up SwiftLint
- Configuring SwiftLint
- Operating SwiftLint through the Xcode construct course of
- Disabling guidelines on a bit of code and entire recordsdata
Homebrew is a software program package deal administration system. It’s a highly regarded device for macOS. For those who haven’t already put in it then go forward and achieve this. Observe the set up instruction for Homebrew in its web site brew.sh.
brew set up swiftlint
Now you can run swiftlint in terminal. On the root of your mission run the next:
By default, SwiftLint is making use of Raywenderlich’s Swift Style Guide. However what if we needed to disable sure guidelines? Or allow guidelines which aren’t enabled? Or perhaps even begin our fashion information from scratch?
To configure swiftlint we should create a configuration file on the root of our mission. The file have to be named
.swiftlint.yml. SwiftLint has a default algorithm primarily based on the RayWenderlich’s swift style guide. Nevertheless we are able to:
- disable guidelines
- opt-in on non-default guidelines
- specify solely the principles you wish to allow (ignoring all default guidelines)
Let’s take a look at examples of how you can obtain every of them.
To disable rules add the next to your
Some guidelines will not be laid out in Raywenderlich Swift style guide nevertheless SwiftLint can examine for extra guidelines. For instance by default SwiftLint is not going to implement implicit returns guidelines:
// Specific return
var id: String return UUID().uuidString
// Implicit return
var id: String UUID().uuidString
To opt-in to rules add the next:
Now SwiftLint will implement implicit returns guidelines.
Lastly, you possibly can utterly ignore Raywenderlich’s Swift style guide in favour of making your individual from scratch.
To create your own rules take away the contents of
.swiftlint.yml and exchange it with the next:
Operating SwiftLint once more could have the identical impact on this instance as earlier than nevertheless now SwiftLint is not going to implement the default settings and ONLY apply
implicit_return as per our configuration file.
For those who’d like to search out out extra about SwiftLint guidelines and whether or not they’re being enforced run the next command:
3. Operating SwiftLint through the Xcode construct course of
Till this level of the tutorial we’ve got been operating SwiftLint by the terminal. The developer should bear in mind to run
swiftlint command. Builders can neglect to run this command and thus submit Pull Requests/Merge Requests with code violating the principles.
swiftlint through the Xcode build process. Open your mission, navigate to your mission settings, then choose your app goal and at last choose Construct Phases tab (Mission settings > App goal > Construct Phases)
Click on on the “+” button then choose “New Run Script Section”. Identify the run script part “Run SwiftLint” and paste the next code:
if which swiftlint >/dev/null; then
echo "warning: SwiftLint not put in, obtain from https://github.com/realm/SwiftLint"
Lastly, transfer the “Run SwiftLint” part after the “Compile Sources” part.
Subsequent from the menu choose Product > Construct.
Xcode will warn the developer of any violations all inside Xcode.
4. Disabling guidelines on a items of code and entire recordsdata
In some eventualities, we’d wish to disable linting on sure strains, items of code (i.e. features), or recordsdata. For all these eventualities we are able to disable linting by including a remark earlier than the code in query akin to the next:
// swiftlint:disable implicit_return
This may disable the
implicit_return guidelines following this line till both remark is added to re-enable it or until the tip of the file. To re-enable the rule we are able to add the next:
// swiftlint:allow implicit_return
We are able to additionally ignore entire recordsdata from linting by together with the next in
Observe: excluding entire recordsdata or directories disables them from all linting guidelines. If you wish to disable sure guidelines then I’d suggest to make use of the feedback disabling methodology as a substitute.