What it is
SwiftLint enforces Swift style and conventions, with hundreds of configurable rules, autocorrection and custom rule support.
Configure rules in .swiftlint.yml and run as a build phase or in CI. Analyzer rules use full type information and run separately.
Installation
brew install swiftlintGetting started
The smallest useful thing you can do with it, and what each part means.
disabled_rules:
- trailing_whitespace
opt_in_rules:
- force_unwrapping # flags every !
- empty_count
- first_where
- explicit_init
- implicitly_unwrapped_optional
excluded:
- .build
- Generated
line_length:
warning: 120
error: 200
ignores_urls: true
custom_rules:
no_print:
regex: '\bprint\('
message: "Use the logger, not print."
severity: warningAdvanced usage
Where the library earns its place over a simpler alternative.
bash
# Xcode build phase — warn without failing the build locally
if which swiftlint > /dev/null; then
swiftlint --quiet
else
echo "warning: SwiftLint not installed"
fi
# CI: fail on any violation
swiftlint --strict
# Analyzer rules need a compiler log — they use type information
swiftlint analyze --compiler-log-path build.logErrors and fixes
The failures you are most likely to hit, and what actually resolves them.
- Violations in generated code
- Add the directory to `excluded`. SwiftLint lints everything under the config root by default.
- Analyzer rules report nothing
- They require swiftlint analyze with a compiler log path.
Best practices
- Enable force_unwrapping and force_try; they surface the crash-prone lines.
- Run --strict in CI but not locally, so development is not blocked by warnings.
- Exclude generated code and .build, or every run drowns in irrelevant violations.
- Always give a reason when suppressing a rule inline.
Background
Why it exists, and what it was reacting to.
From Realm, SwiftLint became the community standard for keeping Swift codebases consistent, and catches a number of correctness issues alongside pure style.
