Start learning free

LESSON 5 OF 415 · WELCOME TO SWIFT

Syntax Errors in Swift: Reading Compiler Messages

Lesson Read the full explanation

TL ; DR:

  • Syntax errors are mistakes in the use of the Swift language, analogous to spelling or grammar mistakes in a language like English. For example:


  • Omitting quotes when working with strings:

    print("Welcome!)
    

  • Omitting parentheses when using the print function:

    print("Let's code!"
    

  • When Swift encounters a syntax error, the compiler reports an error and your program does not run.




Full lesson:

Humans are prone to making mistakes. Humans are also typically in charge of creating computer programs. To compensate, programming languages attempt to understand and explain mistakes made in their programs.

Here are two common errors that we encounter while writing Swift code:


The syntax error:

Syntax errors are mistakes in the use of the Swift language, and are analogous to spelling or grammar mistakes in a language like English. For example, the sentence "Would you some tea?" does not make sense – it is missing a verb.

Syntax error means there is something wrong with the way your program is written. Some examples:

1. Misuse of commas when declaring a string:

var greeting = 'Hey there!"

When we run this code we'll get an error: unterminated string literal.

2. Leaving out a parenthesis:

print"Hey")

When we run this code we'll get errors such as error: consecutive statements on a line must be separated by ';' and error: expected expression.


The reference error:

A reference error occurs when the Swift compiler sees a word it does not recognize. Some examples:

1. Misspelling a class / function name:

prnt("Hey")

When we run this code we'll get an error: cannot find 'prnt' in scope.

2. Using a non-existent variable / Misspelling a variable's name:

let animal = "parrot"
print(aniimal)

When we run this code we'll get an error: cannot find 'aniimal' in scope.


Assignment
Follow the Coding Tutorial and let's practice with errors!


Hint
Look at the examples above if you get stuck.


STEP 1 OF 1
    Ctrl + Enter to check

    SWIFT · WELCOME TO SWIFT

    Syntax Errors in Swift, explained

    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

    1. Read the first error message. Later messages are often consequences of the first one, so fix it and compile again.
    2. Go to the line and column it names, and also look at the line before it.
    3. Count: every opening (, {, [ and " needs a partner.
    4. 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.
    Keep going with SwiftAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free