Swift Syntax
Syntax is the set of rules that defines how a program must be written. Swift's syntax is compact: no semicolons at the end of lines, no parentheses around conditions, and curly braces { } to group code.
A program, line by line
import Foundation
let city = "Hanoi"
let temperature = 31.5
if temperature > 30 {
print("It is hot in \(city) today.")
}
print(String(format: "Exact value: %.1f degrees", temperature))It is hot in Hanoi today.
Exact value: 31.5 degrees
| Line | Meaning |
|---|---|
import Foundation |
Makes Apple's Foundation library available (extra string, date and number tools). Simple programs don't need it. |
let city = "Hanoi" |
Declares a constant. The type String is inferred from the value. |
if temperature > 30 { … } |
A condition — no parentheses needed, but the braces are required. |
print(...) |
Writes a line of text to the console. |
Statements and semicolons
Each statement normally sits on its own line. A semicolon is only needed if you want to put several statements on one line:
let a = 10; let b = 20
print(a + b); print(a * b)30
200
Tip: Putting several statements on one line is legal but rarely used. Swift style guides recommend one statement per line.
Whitespace matters (a little)
Swift is mostly free-form, but operators must have the same spacing on both sides. a + b and a+b both work, but a +b is an error because Swift reads +b as a prefix operator applied to b — so it sees two separate values, a and +b, with no comma between them.
let x = 4
let y = 6
print(x + y)
print(x+y)
print(x +y)error: emit-module command failed with exit code 1 (use -v to see invocation)
<source>:5:9: error: expected ',' separator
3 | print(x + y)
4 | print(x+y)
5 | print(x +y)
| `- error: expected ',' separator
Case sensitivity
Swift is case-sensitive: score, Score and SCORE are three different names.
| Convention | Used for | Example |
|---|---|---|
lowerCamelCase |
variables, constants, functions | userName, totalPrice() |
UpperCamelCase |
types (struct, class, enum, protocol) | ShoppingCart, Direction |
Code blocks and indentation
Curly braces mark the start and end of a block. Indentation is not required by the compiler, but everybody uses 4 spaces so the structure is easy to see:
let numbers = [3, 8, 1]
for n in numbers {
if n > 2 {
print("\(n) is bigger than 2")
} else {
print("\(n) is small")
}
}3 is bigger than 2
8 is bigger than 2
1 is small
Keywords
Some words are reserved by the language and cannot be used as names: let, var, func, if, else, for, while, return, struct, class, enum, import, true, false, nil and many more. If you really need one as a name, wrap it in backticks: let `class` = "Swift 101".
Test yourself
When do you need a semicolon in Swift?
- At the end of every statement
- Only to separate several statements on the same line
- After every closing brace
- Never — semicolons are forbidden
Which naming style does Swift use for type names?
- snake_case
- lowerCamelCase
- UpperCamelCase
- SCREAMING_CASE
Why does print(x +y) fail to compile?
printcannot add numbers- Operators need the same whitespace on both sides
ymust be declared withvar- Swift requires parentheses around
x + y