Swift Code Formatter
Format and beautify Swift source: re-indent by brace depth, normalize spacing around operators and punctuation, strip redundant semicolons, add trailing commas to multi-line lists, and sort import statements — all in your browser.
Input
Style Options
Output
Guides
Paste your Swift source and get it cleanly re-indented and spaced in one click. The Swift Code Formatter tidies up messy, minified, or inconsistently indented Swift — structs, classes, protocols, enums, closures, and functions — so it reads the way Apple's own style guides intend. Everything runs in your browser; your code is never uploaded to a server.
What it does
The formatter walks your code line by line and rebuilds the indentation from the nesting of braces {}, parentheses (), and brackets []. On top of re-indenting, it normalizes the small spacing details that make Swift readable:
- Operators — single spaces around
=,==,!=,<=,>=,===,!==, compound assignments (+=,-=, …), the logical&&and||, the return/closure arrow->, and the nil-coalescing??. - Punctuation — a space after commas and after
:in type annotations and dictionary literals, no space before;,,,), or], and a single space before an opening{. - Comments and strings are protected — spacing rules only touch real code. Text inside string literals (including multi-line
"""…"""strings) and inside//,///, and/* … */comments is preserved exactly.
Options
- Indent size — 4 spaces (Swift's default), 2 spaces, or tabs.
- Sort imports alphabetically — reorders a contiguous block of
importstatements (including@testable,public, and other access-modified imports) by module name. - Add trailing commas in multi-line lists — appends a trailing comma to the last item of an argument list or collection literal that closes on its own line, which keeps diffs clean when items are added.
- Remove redundant semicolons — strips the trailing
;that Swift doesn't require at the end of a statement.
It also inserts a single blank line between adjacent top-level declarations and collapses runs of three or more blank lines down to one.
How to use it
- Paste your Swift code into the input box (or upload a
.swiftfile). - Choose your indent size and toggle the options you want.
- The formatted result appears instantly on the right — copy it or download it as
formatted.swift.
Is this a full Swift formatter?
No. This is a fast, heuristic re-indenter and spacing normalizer — not a full parser like Apple's swift-format or SwiftSyntax. It doesn't reflow long lines, reorder declarations, or verify that your code compiles. It's built for the common case: quickly cleaning up indentation and spacing on a snippet you've pasted or generated. Because it never parses a full grammar, it's forgiving of incomplete fragments and leaves anything it doesn't understand untouched.
Is my code private?
Yes. All formatting happens locally in your browser using JavaScript — nothing is sent to a server, so it's safe to paste proprietary code.
Use it from code
From 3 credits per callREST API
curl -X POST https://api.iotools.cloud/v1/tool/swift-code-formatter \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"swiftInput": "import Foundation\nimport SwiftUI\nimport Combine\n…",
"indentSize": "4",
"sortImports": "true",
"trailingCommas": "true",
"stripSemicolons": "true"
}'Swap in your own key from your account. The tool's fields are the body — no wrapper.
Ask an AI agent
Use the IOTools `swift-code-formatter` tool (Swift Code Formatter) on this input:
YOUR_INPUT_HEREPaste this at any agent connected to the IOTools MCP server, then add your input.