>_ DevTrendsen

Language

Home

Languages

Sections

Frontend Backend Mobile DevOps AI / ML
Swift

SwiftFormat: The Automatic Stylist for Your Swift Code

8,841 stars

SwiftFormat logo

When Code Formats Itself

Imagine: in a team of five developers, everyone writes code their own way. Some add spaces before brackets, some don't. One prefers breaking function parameters across lines, another writes everything on a single line. The result is a patchwork codebase, and code review turns into endless style debates.

SwiftFormat solves exactly this problem — a tool for automatic Swift code formatting. It doesn't just add spaces and indentation, it brings code to a unified standard, fixing hundreds of minor inconsistencies.

What Can SwiftFormat Do?

  1. Deep style work:

    • Corrects indentation and alignment
    • Removes redundant self and brackets
    • Fixes access modifier order
    • Unifies closure declaration style
  2. Flexible configuration:

    • More than 50 formatting rules
    • Support for configuration files .swiftformat
    • Ability to create custom rules
  3. Integration anywhere:

    • Command line for manual execution
    • Plugins for Xcode, VSCode, Sublime Text
    • Git pre-commit hooks
    • CI/CD support via GitHub Actions
// До форматирования
func processData(data:[String:Any], completion:(Result)->Void){
    guard let value=data["key"] else{return}
    self.completion(.success(value))
}

// После SwiftFormat
func processData(
    data: [String: Any],
    completion: (Result) -> Void
) {
    guard let value = data["key"] else { return }
    completion(.success(value))
}

How to Get Started?

Installation via Homebrew:

brew install swiftformat

Basic usage:

# Форматировать все файлы в текущей директории
swiftformat .

# Только проверить стиль без изменений (линтер)
swiftformat --lint .

For projects, it's recommended to add SwiftFormat as a build step in Xcode or a pre-commit hook in Git. This way, code will be automatically formatted before each commit.

Configuration Flexibility

SwiftFormat understands that every project has its own style requirements. You can:

  • Disable individual rules via --disable
  • Create a .swiftformat file at the project root
  • Configure behavior for specific files via comments

Configuration example:

# .swiftformat
--indent 4
--disable trailingCommas
--enable isEmpty
--header "Copyright (c) {year} MyCompany"

Why Try It?

  1. Time savings — no longer spend hours on manual formatting
  2. Consistent style — no more team debates about where to place brackets
  3. Clean git history — fewer "style" commits
  4. Focus on logic — concentrate on what really matters

SwiftFormat is especially useful for:

  • Teams of 2+ developers
  • Projects with a long development history
  • Frequent code reviews
  • Maintaining open source project quality

Under the Hood

The project has been actively developed since 2016, has over 8.5k stars on GitHub, and supports all current Swift versions. Under the hood is a powerful Swift code parser and a rule system that you can combine like LEGO blocks.

# Показать все доступные правила
swiftformat --rules

# Информация о конкретном правиле
swiftformat --rule-info redundantSelf

Alternatives

  • SwiftLint: More focused on finding code problems than formatting
  • Crafted scripts: Require maintenance and are often less flexible

SwiftFormat's main advantage is that it doesn't just find problems — it fixes them immediately.

SwiftFormat isn't just "another tool" — it's a real lifesaver for teams tired of endless style fixes. It takes care of all the routine formatting work, allowing you to focus on solving real problems.

Try adding it to your workflow — you'll see results after the very first run. Your code will become cleaner, and life — easier.