Swift Formatter

Formatters

How to use the Swift Formatter

Format Swift code in three steps:

1

Paste or upload Swift code

Paste Swift source code into the input panel or upload a .swift file. SwiftUI views, UIKit controllers, or any Swift code is accepted.

2

Choose indent size

Select 2 or 4 spaces. SwiftFormat defaults to 4 spaces; select your project's configured indent width.

3

Format, copy, or download

Click 'Format Swift' to apply SwiftFormat-style indentation and Swift syntax highlighting. Copy the result or download as a .swift file.


When to use this tool

Use the Swift formatter when you need clean, consistently indented Swift code without running Xcode:

  • Formatting Swift code from Apple documentation, WWDC session samples, or GitHub that lost indentation
  • Cleaning up SwiftUI view code from AI assistants before integrating into an Xcode project
  • Preparing Swift examples for iOS developer blog posts, tutorials, or GitHub README files
  • Formatting Swift code snippets for code review comments without opening Xcode
  • Tidying Swift Package Manager library source code before publishing to a public repository
  • Formatting Swift interview solutions or Hacking with Swift examples for readable submission

Frequently asked questions

Q:Does Swift use K&R or Allman brace style?
Swift uses K&R style by convention — opening braces appear on the same line as the declaration, not on a new line. This is the style enforced by SwiftFormat and the Swift API Design Guidelines. The Swift formatter uses same-line braces consistent with this convention.
Q:Does it support SwiftUI property wrappers like @State, @Binding, and @Published?
Yes — SwiftUI and Combine property wrappers (@State, @Binding, @ObservedObject, @EnvironmentObject, @Published, @StateObject, @Environment, @AppStorage, @SceneStorage, @FetchRequest) are recognised as attributes and highlighted in orange. They are indented at the correct level within their surrounding struct or class body.
Q:Does it handle Swift concurrency — async, await, actor, and nonisolated?
Yes — async, await, actor, nonisolated, and the @MainActor attribute are all recognised Swift keywords and are correctly handled in indentation and highlighted in the syntax output. Async function bodies, actor-isolated methods, and Task closures are indented as standard Swift code blocks.
Q:Can it format Swift code using result builders for SwiftUI views?
Swift result builders (used by SwiftUI's @ViewBuilder and @resultBuilder) create nested view hierarchies using plain Swift block syntax. The formatter indents the view body correctly as it would any closure block. Complex view hierarchies with HStack, VStack, ZStack, and modifier chains are indented relative to their parent view.
Q:Does it work with Swift Package Manager package manifests (Package.swift)?
Yes — Package.swift files are valid Swift source files and are formatted identically to application code. The formatter handles the package manifest's target and dependency declarations, which use Swift's result-builder-like nested syntax, as standard Swift code blocks.
Q:How does this compare to running SwiftFormat or the Xcode formatter?
This browser formatter applies SwiftFormat's block indentation rules accurately for most practical code. Full SwiftFormat also handles trailing commas, self-reference rules, redundant type annotations, blank line insertion between declarations, and 150+ other code style rules. Use this tool for quick snippet formatting; use SwiftFormat with your Xcode project for complete style enforcement.