Swift Formatter
Re-indent Swift and SwiftUI at two spaces per level from brace depth, so nested closures and guard blocks line up again.
Input
Output
What is Swift?
Swift is a powerful, modern programming language developed by Apple for iOS, macOS, watchOS, and tvOS development. You can read the full language reference in The Swift Programming Language book. It is designed to be safe, fast, and expressive: learn more on Wikipedia.
Indentation here is two spaces per level, worked out from bracket depth: a line that ends in {, ( or [ opens a level and a line that starts with a closer ends one. That is enough for nested VStack closures and guard ... else { blocks. Naming is still on you; the Swift API Design Guidelines are the reference for that, and this tool never renames anything.
How to Use
Paste your Swift code
Paste a view, a model or a whole file on the left, or load a .swift file with Upload. Sample fills in a Codable struct and a small UserService class.
Formats as you type
Pause typing for about 300 ms and the right pane refreshes. Each line is trimmed and moved two spaces in per open brace, bracket or paren, and stacks of blank lines collapse to a single one.
Copy or download
Paste the result back into Xcode with Copy, or save it as main.swift with Download. Xcode's own Re-Indent defaults to four spaces, so expect a whitespace diff if your project kept that setting.
Formatting Example
Example: Format a Swift struct
Unformatted input:
struct User {
var name: String
var age: Int
func greet() -> String {
"Hello, \(name)!"
}
var isAdult: Bool {
age >= 18
}
}Formatted output:
struct User {
var name: String
var age: Int
func greet() -> String {
"Hello, \(name)!"
}
var isAdult: Bool {
age >= 18
}
}Frequently Asked Questions
Does this formatter handle Swift closures?
Yes. Swift closures use brace-based syntax and are handled by the formatter's brace indentation logic. Trailing closure syntax and multi-line closures are properly indented.
Can I format SwiftUI code?
Yes. SwiftUI code follows standard Swift syntax. Paste your SwiftUI view code and the formatter will normalize indentation across all struct, var body blocks, and modifier chains.
Does this replace swift-format?
This is a lightweight online indentation formatter. For local development, Apple's SwiftFormat tool provides full style enforcement with many more rules. This tool is ideal for quick formatting without any installation.
Is my Swift code safe when I use this tool?
Yes. Formatting is a single pass over the lines in this page's script and makes no network call, so an AppConfig.swift holding an API key does not leave your Mac. The file behind Download is assembled locally too.