Curly brackets
In Swift, unlike other C-like languages, curly brackets are required for conditional statements and loops. In other C-like languages, if there is only one statement to execute for a conditional statement or a loop, curly brackets around that line are optional. This has led to numerous errors and bugs, such as Apple's goto fail
bug. When Apple was designing Swift, they decided to introduce the use of curly brackets, even when there was only one line of code to execute. Let's take a look at some code that illustrates this requirement. This first example is not valid in Swift because it is missing the curly brackets; however, it will be valid in most other languages:
if (x > y)
x=0
In Swift, you are required to have curly brackets as illustrated in the following example:
if (x > y) {
x=0
}