menu

Swift Style Guidelines

Punctuation

Semicolons

Don’t use semicolons. If you think you need to use semicolons, try to find a different way to do it.

Sole Exception

There is actually one case where semicolons are acceptable in the code: When a scope opens and you are logging both the entrance and exit of that scope. In this case, you may place both these log lines and the defer block on the same line of code.

Good Acceptable
func importantFunction() {
    logFunctionEntry()
    defer {
        logFunctionExit()
    }
}
func importantFunction() {
    logFunctionEntry(); defer { logFunctionExit() }
}

Braces

Along with Apple’s guidelines, curly braces ({ and }) open on the same line and close on a new line. Curly braces that are the first non-whitespace character on a line must be indented to the same level as the code around its block, not inside it.

The main difference between our guidelines and Apple’s is the else statement; we place them on the line following their if’s closing brace, whereas Apple places them on the same line.

Braces are required, by all guidelines and by the compiler, for all if, else, guard, switch, while, and for statements.

Good Bad
class Foo {
    func bar() {
        if baz {
            qux.hoge()
        }
        else {
            qux.norf()
        }
    }
}
class Foo
{ // Opening curly braces belong on the same line
    func bar()
        { // Indent standalone braces along with the code AROUND the block, not inside
        if baz {
            qux.hoge()
            } // Indent standalone braces along with the code AROUND the block
        else {
            qux.norf()} // This obfuscates the end of the block
    }
}
if foo {
    bar()
}
else if let baz = self.baz {
    baz.qux()
}
else {
    hoge()
}
if foo {
    bar()
} else if let baz = self.baz { baz.qux() }
else
{
    hoge()
}

Parentheses

RULE-OF-THUMB When in doubt, more parentheses is better.

if, else, guard, switch, while, and for statements should not use parentheses unless necessary for compiling, or if their logic is so complex that parentheses are required for clear human andor compiler understanding. If this logic is very complex, consider moving it into its own function.

Always strive for clarity; when using multiple operators (mathematical, Boolean, or otherwise), use parentheses to explicitly group each pair. This ensures it does what you think it does, and future readers know what you intended it to do.

Good Bad
if foo {
    bar()
}
else if baz, qux {
    hoge()
}
else if norf {
    foobar = ((2 + (6 / foobar)) * 3)
}

// ...

var norf: Bool {
    return (!foo && baz)
            || (qux && !baz)
}
if (foo) {
    bar()
}
else if (baz && qux) {
    hoge()
}
else if !foo && baz || qux && !baz {
    foobar = 2 + 6 / foobar * 3
}