Swiftlint
Swiftlint is a linter. its a tool that will help us to identify parts of code that is not following community standards or our custom styling guide.
Why we use swiftlint
Consistency within a single project
Consistency between projects
Consistency across teams
To ensure all these consistency. We have a small swift style guide for our team but still we forgot to follow these rules. this linter will enforce you to follow style guide.
Installation
- Using Homebrew
$ brew install swiftlint
- Using CococaPods
Simply add the following line to your Podfile:
but we will use Homebrew methodpod 'SwiftLint'
Running
We have two options to run swiftlint
- Terminal
go to project directory and type following command
$ swiftlint
- Integrate SwiftLint with Xcode
Add a new "Run Script Phase" to your project's default target.
To add a new "Run Script Phase" select Target, Build Phases, click on + icon on top side and select New Run Script Phaseif which swiftlint >/dev/null; then swiftlint else echo "warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint" fi
Now build your project.
Swiftlint will lint using default rules, if you want to customize the rule place a .swiftlint.yml file in project directory
we will use our Default swiftlint file. which is written on our swift style guide
Autocorrect
Swiftlint has a feature of autocorrect, which comes handy to correct some silly mistakes in our code
to use this feature type following in terminal
$ swiftlint autocorrect
Some types of violation that can be fixes with autocorrect
- Closing brace.
- Colon.
- Comma Spacing.
- Opening Brace Spacing.
- Trailing Newline.
- Trailing Semicolon.
for more type following in terminal
$ swiftlint rules