talira

SwiftFormat

A command-line tool and Xcode extension for formatting Swift code

open sourceMITfree

SwiftFormat is a code library and command-line tool that reformats Swift code on macOS, Linux and Windows. Beyond whitespace, its 150-plus rules insert or remove implicit self, remove redundant parentheses and fix many other departures from standard Swift idioms. It runs as a CLI, an Xcode source editor extension, an Xcode build phase, a Swift Package Manager plugin, a Git pre-commit hook, a Docker image or a GitHub Actions step, and its --lint mode checks code against the same rules in CI without changing it. Options can be kept in a .swiftformat config file.

Ways to install

Problems it solves

  • format my Swift code automatically to one consistent style
  • enforce a Swift code style on every pull request in CI
  • reformat Swift files in Xcode on save or with a menu command

Features

  • over 150 formatting rules, each documented
  • Xcode source editor extension and build phase
  • Swift Package Manager plugin
  • lint mode for CI with GitHub Actions log reporter
  • .swiftformat config file and per-line disable comments
  • Docker image and prebuilt binaries

Limitations

  • Some rules can change behaviour or break compilation in edge cases (enumNamespaces, redundantType, preferKeyPath and others), as its known issues list says3
  • Releases are no longer published to CocoaPods since version 0.64.03
  • In Xcode 15 or later, the build phase needs User Script Sandboxing turned off3

Alternatives

The same job

These do what SwiftFormat does.

  1. Airbnb Swift Style Guide Airbnb's Swift style guide, with a SwiftPM plugin that formats and lints to it

Compare SwiftFormat with the top 3