To display SwiftLint warnings and errors directly in the Xcode IDE, add a new "Run Script Phase" to your target's "Build Phases" tab.
Standard Script
Use this script to check if SwiftLint is installed before running it:
if which swiftlint >/dev/null; then
swiftlint
else
echo "warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint"
fi
For Apple Silicon (M1/M2/M3) Users
If you installed SwiftLint via Homebrew on Apple Silicon, you must add /opt/homebrew/bin to your PATH so Xcode can locate the binary:
if [[ "$(uname -m)" == arm64 ]]; then
export PATH="/opt/homebrew/bin:$PATH"
fi
if which swiftlint > /dev/null; then
swiftlint
else
echo "warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint"
fi
Alternatively, you can create a symbolic link to /usr/local/bin:
ln -s /opt/homebrew/bin/swiftlint /usr/local/bin/swiftlint
Using CocoaPods Installation
If you installed SwiftLint via CocoaPods, use this script in your Build Phase instead:
"${PODS_ROOT}/SwiftLint/swiftlint"
Pro-tips
- Auto-fix violations: To automatically fix fixable violations and then show remaining warnings, use
swiftlint --fix && swiftlint in your script. - Execution Order: It is recommended to run SwiftLint after the 'Compile Sources' phase. Running it before compilation might result in inaccurate errors because SwiftLint is designed to work with valid Swift code that has undergone parsing.