ModernCalcs

Swift Formatter

Indent
import Foundation

protocol Greetable {
    var name: String { get }
    func greet() -> String
}

struct User: Greetable {
    let name: String
    let age: Int
    var isAdult: Bool { age >= 18 }
    func greet() -> String {
        return "Hello, \(name)!"
    }
}

class UserManager {
    private var users: [User] = []
    func add(_ user: User) {
        users.append(user)
    }
    func adults() -> [User] {
        return users.filter { $0.isAdult }
    }
    func greetAll() {
        for user in users {
            print(user.greet())
        }
    }
}

let manager = UserManager()
manager.add(User(name: "Alice", age: 30))
manager.add(User(name: "Bob", age: 16))
manager.greetAll()
print("Adults:", manager.adults().count)

Swift style guide recommends 4-space indentation. For Xcode-standard formatting, use Editor → Structure → Re-Indent (⌃I) or install swift-format for CI pipelines.

Swift Formatter: Indent and Beautify Swift Code

Swift is Apple's language for iOS, macOS, and server-side development. This formatter applies consistent brace-depth indentation to Swift code — helpful for reviewing AI-generated Swift, reading documentation samples, or cleaning up pasted code before opening it in Xcode.

Formula
// Input: flat class UserManager { private var users: [User] = [] func add(_ user: User) { users.append(user) } func adults() -> [User] { return users.filter { $0.isAdult } } } // Output: indented class UserManager { private var users: [User] = [] func add(_ user: User) { users.append(user) } func adults() -> [User] { return users.filter { $0.isAdult } } }

Swift style guides recommend 4-space indentation. Trailing closures with balanced braces on a single line don't affect surrounding indentation.

Swift's Type System and Blocks

Swift uses { } for class, struct, enum, protocol, extension, function, if/else, guard, for, while, switch case, and closure bodies. The formatter tracks all of these uniformly. Computed properties (var name: Type { ... }) also use { } and are indented correctly.

Trailing Closures and SwiftUI

Swift's trailing closure syntax places { } at the end of a function call. Single-line trailing closures have balanced braces on one line and don't change indentation. Multi-line closures work like any other block.

swift-format for Production Code

For enforced formatting in a Swift project, use swift-format (Apple's official tool) or SwiftLint. Xcode also has built-in re-indent (⌃I). swift-format can be integrated into CI to reject unformatted code at the PR level.

Practical Examples

Formatting a Swift Struct from Documentation

Format a flat Swift struct definition from Apple documentation or a tutorial.

  • 1.Paste the Swift code
  • 2.Formatter applies 4-space indentation (Swift standard)
  • 3.Review properties, computed vars, and function signatures
  • 4.Copy into Xcode to continue implementing

What Gets Formatted

  • class, struct, enum, protocol, extension, func blocks
  • if/else, guard, for, while, switch, and closure blocks
  • Computed property { } blocks
  • 4-space indentation default (Swift standard)

Good Use Cases

  • Formatting AI-generated Swift code
  • Making Swift tutorials and documentation readable
  • Quick review before opening a snippet in Xcode
  • Teaching Swift to developers from Java or Kotlin

Frequently Asked Questions

What indentation does the Swift style guide recommend?

The Swift API Design Guidelines and most Swift style guides recommend 4-space indentation. 2-space indentation is sometimes used in projects with heavily nested closures.

Does this handle Swift closures?

Closures use { } blocks and are indented like any other block. Trailing closure syntax (array.filter { $0.age > 18 }) has balanced braces on the same line and does not affect the indentation of surrounding code.

How do I format Swift in Xcode?

Use Editor → Structure → Re-Indent (⌃I / Control+I) to reindent the selected code or file. For automatic formatting on save and stricter style enforcement, install swift-format or SwiftLint.

Does SwiftUI code format well?

SwiftUI uses result builder syntax that chains view modifiers. The brace-depth formatter handles { } in SwiftUI (body, computed properties, ForEach closures) but does not reformat modifier chains.