Developer and Text Tools

Formatting Kotlin and Swift: ktlint and swift-format Rules Side by Side

ktlint and swift-format rules compared: indentation, colons, trailing commas, imports and semicolons, and a way to apply them in your browser.

Saizul Amin
Saizul Amin7 Nov 2026 · 3 min read
Formatting Kotlin and Swift: ktlint and swift-format Rules Side by Side

Teams that build both Android and iOS apps often keep the Kotlin and Swift code in the same repository, and reviewers move between the two all day. The languages look similar enough that small style differences become confusing. Is there a space before the colon? Are trailing commas allowed? Which imports go first? The Kotlin and Swift Code Formatter applies the ktlint and swift-format conventions to both kinds of file in one place, and this guide explains the rules behind it.

Indentation

ktlint follows the Kotlin coding conventions with four spaces per level. swift-format defaults to two spaces, which surprises people coming from Xcode, whose own default is four. The tool offers both for Swift: the swift-format preset with two spaces and an Xcode preset with four.

Continuation lines, such as a chain of .filter and .map calls written one per line, are indented one extra level in both languages, so the start of the statement stays visible.

Colons

In Kotlin, a colon in a type annotation sits right after the name, as in val count: Int. In a class header that lists supertypes, ktlint wants spaces on both sides: class Repository : Base(). The same applies to generic constraints such as <T : Any>. Swift uses the tight form everywhere, so the supertype list reads class Cart: Codable. Dictionary types and literals in Swift also use the tight colon, as in [String: Int].

Trailing commas

ktlint's official style adds a trailing comma after the last item of any list that is split across lines, whether it is a parameter list, an argument list or a collection. The benefit shows up in code review: adding a new parameter changes one line instead of two. The intellij_idea and android_studio styles leave commas as you wrote them.

swift-format adds trailing commas too, but only in multi-line array and dictionary literals, because older Swift versions did not accept them in argument lists. The formatter follows that rule and can also remove trailing commas if your project prefers none.

Imports

ktlint's default layout puts every import in one block sorted lexicographically, except that java, javax and kotlin imports go after the others, and alias imports written with as go at the very end. There are no blank lines inside the block. Wildcard imports are reported, since the no-wildcard-imports rule rejects them.

swift-format sorts imports into up to three groups: regular imports such as import UIKit, declaration imports such as import struct Foundation.Date, and @testable imports used in test targets, with a blank line between groups.

Semicolons

Neither language needs semicolons at the end of a statement, and both tools remove them. There is one exception in Kotlin: an enum class whose entries are followed by functions or properties needs a semicolon after the last entry, and the formatter keeps that one.

Strings stay safe

String templates are where generic formatters go wrong. A Kotlin template such as "${user.first} ${user.last}" can contain braces and even other strings, and a Swift interpolation like "\(item.name)" can contain parentheses and quotes. Both tokenizers read these as part of the string, along with Kotlin raw strings, Swift multi-line strings and extended raw strings such as #"..."#, so nothing inside them changes.

Working through a project

Open your .kt, .kts and .swift files together. Each file is formatted with the options of its own language, so you can set ktlint_official for Kotlin and swift-format for Swift once and review both. The diff shows each change, the issues tab lists unbalanced brackets, wildcard imports and force unwraps, and you can apply the result to everything and download one ZIP.

The engines keep your line breaks and do not wrap long expressions. For that, keep running ktlint and swift-format in your build; the browser tool is for quick clean-ups, reviews and machines where those tools are not installed.

Completely freeNo cost, no sign-up
Files stay privateEverything runs in your browser
Easy in your languageAvailable in many languages