Is there any technical benefit in adding semicolons in Swift?

There are (or used to be) situations where two separate statements on two different lines would taken by the compiler as a single statement. The typical locus is return followed by another statement that you don't want executed. In that situation, and in that situation alone, the semicolon is crucial.

override func viewDidLoad() {
    super.viewDidLoad()
    return // need semicolon here
    print(1) // oops
}

However, in modern Swift the compiler warns and compensates. Adding a semicolon (return;) quiets the warning (though it then causes a different warning).

Otherwise, a semicolon never has any technical benefit.


Yes there is, you can write multiple statements in one line. For example you could write statements more compact when needed, for example:

var progress: Progress {
    get { _progress.value }
    set { _progress.value = newValue; parent?.update() }
}

Use sparingly though. But in this specific case I prefer it instead of expanding everything to a separate line since its still quite readable and only 2 statements.


The short answer is: No.

Swift does not require a semicolon after each statement in your code. They are only required if you wish to combine multiple statements on a single line.

Do not write multiple statements on a single line separated with semicolons.

Preferred:

var swift = "not a scripting language"

Not Preferred:

var swift = "not a scripting language";

For more informations, please refer to this page:
https://github.com/raywenderlich/swift-style-guide#semicolons

Tags:

Syntax

Swift