What a syntax error is
A syntax error is a mistake in how the code is written, like a grammar mistake in English. "Would you some tea?" is missing a verb, and a reader notices at once. Swift is just as strict about its own grammar: a missing quote, bracket or comma makes a line impossible to understand.
Swift is a compiled language. Before a program runs, the compiler reads all of it. If it finds a syntax error, it prints an error message with the file position and does not run the program at all. This is different from a mistake that appears halfway through a run: with a syntax error, even the lines before the mistake print nothing.
Common syntax errors
A missing quote
print("Welcome!)
The string opens with " and never closes, so the compiler reports unterminated string literal. Everything after the opening quote, including the closing parenthesis, looks like part of the text.
The wrong kind of quote
var greeting = 'Hey there!"
The text opens with a single quote and ends with a double quote. The compiler reports unterminated string literal here too. Swift text uses double quotes at both ends, and a text in single quotes alone is rejected with a message that tells you to use ".
A missing parenthesis
print("Hello!"
print("I love coding!")
The first call is never closed. The compiler reads on to the next line, looking for the end of the argument list, and reports expected ',' separator at the start of line 2, with a note pointing back to the opening parenthesis it was trying to match. The error is shown where the compiler gave up, which can be a line after the real mistake. When a message points at one line, look at the line above it too.
The opposite mistake is just as bad. In print"Hey") the opening parenthesis is missing. The compiler reports that consecutive statements on a line must be separated by ;, along with expected expression. The wording of a message is not always a clear description of the real mistake, but its position almost always is.
Errors that are not syntax errors
Using a name that does not exist, such as prnt("Hey"), is not a grammar mistake. The line is well formed, but Swift cannot find what it refers to, and reports cannot find 'prnt' in scope. The compiler still stops before running, but the fix is different: correct the spelling instead of the punctuation. The next lesson covers this kind of error.
How to fix one
- Read the first error message. Later messages are often consequences of the first one, so fix it and compile again.
- Go to the line and column it names, and also look at the line before it.
- Count: every opening
(,{,[and"needs a partner. - Change one thing, then run again.
The exercise
The exercise shows two print lines that should print Hello! and I love coding!, but each has a small mistake:
print("Hello!"
print("I love coding!)
On the first line, the call is missing its closing parenthesis. On the second, the string is missing its closing quote. Add the missing parts:
print("Hello!")
print("I love coding!")
Now each quote and parenthesis has a partner, the compiler accepts the program, and it prints two lines:
Hello!
I love coding!
Before the fix, the compiler reports two errors, both on line 2: expected ',' separator at the start of the line, and unterminated string literal where the string opens. Once both are fixed, the messages disappear together.
Key takeaways
- A syntax error is a grammar mistake in the code.
- Swift finds it when it compiles, so nothing runs until it is fixed.
- Missing quotes and parentheses are the most common causes.
- The reported line can be one past the actual mistake.
- Fix the first message first.
