This guide will help you set up SwiftFormat and SwiftLint in your Swift project for automated code formatting and linting.
chmod +x format_and_lint.sh
Install dependencies
./format_and_lint.sh
Clone or download the necessary files from the https://github.com/gonz4216/Fortmat-and-lint-for-Swift.
Copy the following files from the downloaded repository to your Swift project's directory:
.swift-version
.swiftformat
format_and_lint.sh
README.md
-
Open your Xcode project.
-
In the Xcode project navigator, select your project.
-
Go to the project's target (the one you want to lint) and select "Build Phases."
-
Click the "+" button and choose "New Run Script Phase."
-
In the newly created "Run Script" phase, add the following script:
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
-
Open Terminal.
-
Navigate to your project directory using the
cd
command. -
Provide execute permissions to the format_and_lint.sh script:
chmod +x format_and_lint.sh
- In Terminal, within your project directory, run the script:
./format_and_lint.sh
You've successfully set up SwiftFormat and SwiftLint in your Swift project. Now, enjoy coding with automated code formatting and linting!