iTranslated by AI
Using the Built-in swiftlang/swift-format in Xcode
Summary
Command Line Interface (CLI)
# After confirming that
# Xcode 16.0
# Build version 16A242d or later with `xcodebuild -version`
swift format --help
When using xcrun
# After confirming that
# Xcode 16.0
# Build version 16A242d or later with `xcodebuild -version`
xcrun --run swift-format --help
# Or use `--find`
$(xcrun --find swift-format) --help
Xcode Menu

Editor > Structure > Format File with 'swift-format' (Ctrl + Shift + I)
What is swiftlang/swift-format?
swiftlang/swift-format (formerly apple/swift-format) is a code formatter and linter that can be used in projects using Swift.
It is located under swiftlang and can be considered the official formatter.
Conventional Installation Methods for swiftlang/swift-format
swiftlang/swift-format could be used as a command-line tool, a Swift Package plugin (command plugin), and more. Examples of using it as a command-line tool are as follows:
git clone https://github.com/swiftlang/swift-format
cd swift-format
git checkout refs/tags/600.0.0 # For Swift 6.0
swift package experimental-install --product swift-format
brew install swift-format
mint install swift-format
How to Use swiftlang/swift-format as a Command-Line Tool
To display the help, use --help.
# If added to your PATH
swift-format --help
# If not added to your PATH (Swift Package Manager example)
~/.swiftpm/bin/swift-format --help
# If not added to your PATH (Homebrew example)
/opt/homebrew/bin/swift-format --help
# If Mint's path is set
mint run swift-format --help
For detailed usage, refer to the output of --help, the swiftlang/swift-format README, or other explanatory articles.
🎉 swiftlang/swift-format is now included in Xcode's built-in toolchain
Starting from Xcode 16, released to the general public on September 17, 2024 (JST), swiftlang/swift-format is now bundled with the built-in toolchain. As a result, if you want to use it as a command-line tool, you no longer need to install it via Swift Package Manager, Homebrew, or Mint, provided you have Xcode installed.
After installing Xcode 16.0 (16A242d) or later through your preferred method (e.g., App Store), verify that version 16.0 (16A242d) or later is active using xcodebuild.
xcodebuild -version
# Xcode 16.0
# Build version 16A242d
By using the --find option of xcrun, you can output the path of the swiftlang/swift-format included in the built-in toolchain.
xcrun --find swift-format
# /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-format
Using the swiftlang/swift-format in Xcode as a Command-Line Tool
By using swift format, you can execute the swiftlang/swift-format from Xcode's built-in toolchain.
# After confirming that
# Xcode 16.0
# Build version 16A242d or later with `xcodebuild -version`
swift format --help

When using xcrun
By using the --run option of xcrun, you can execute the swiftlang/swift-format from Xcode's built-in toolchain.
# After confirming that
# Xcode 16.0
# Build version 16A242d or later with `xcodebuild -version`
xcrun --run swift-format --help
# Or use `--find`
$(xcrun --find swift-format) --help

In scripts where you previously executed swift-format, you can switch to using the version in Xcode's built-in toolchain by replacing it with swift format.
Using swiftlang/swift-format from the Xcode Menu
To perform formatting using swiftlang/swift-format while editing files in the Xcode editor, click "Editor" > "Structure" > "Format File with 'swift-format'" each time. Alternatively, you can execute it anytime using Ctrl + Shift + I.

Editor > Structure > Format File with 'swift-format' (Ctrl + Shift + I)
Discussion
swift formatで済ませるのが短くて良いと思います。ご指摘ありがとうございます!
初稿での
xcrunを使う方法についてはアコーディオンに閉じ、swift formatを使う方法へと更新させていただきました。.swiftformatでしたっけ、そういう設定ファイルを読んでくれるのか気になってました。
調べてみよう
読んでくれましたわ
.swift-format
例えばこんな設定で