Start learning free

LESSON 6 OF 415 · WELCOME TO SWIFT

Name Errors in Swift: "cannot find in scope"

Lesson Read the full explanation

TL ; DR:

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

  • Misspelling a function name:

    prnt("Heyooo!") // error: cannot find 'prnt' in scope
    

  • Misspelling a variable name:

    let animal = "parrot"
    print(anmal) // error: cannot find 'anmal' in scope
    

  • When Swift encounters a name 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:

let 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:

prntln("Hey")

When we run this code we'll get an error: cannot find 'prntln' 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

    Name Errors in Swift, explained

    What a name error is

    Every name in Swift code has to refer to something: a function such as print, a variable you declared, a type, a method. When the compiler meets a name it cannot match to anything, it stops with an error. Swift has no error called NameError; the message you will see is:

    error: cannot find 'prnt' in scope
    

    "In scope" means "in a place where that name is defined and visible". The code around the mistake is fine grammatically, so this is not a syntax error. The name is simply unknown.

    Swift checks names while compiling. If any name is unknown, the program is rejected as a whole and nothing runs, not even the lines before the mistake.

    Misspelled function names

    prnt("Heyooo!")
    

    There is no function called prnt, so the compiler reports cannot find 'prnt' in scope. Capitalisation counts as well: Print("Hi") gives cannot find 'Print' in scope, because Swift names are case-sensitive.

    Misspelled variable names

    let animal = "parrot"
    print(aniimal)
    

    The variable is called animal, but the second line spells it aniimal. The compiler reports cannot find 'aniimal' in scope, and the message points at line 2, the line that has the wrong spelling, not at the declaration.

    Other ways to get the same error

    • Using a name outside its scope. A constant declared inside { }, such as the body of an if or a function, is not visible after the closing brace.
    • Forgetting a declaration. The code uses total, but the line var total = 0 was never written.

    The scope case looks like this:

    if true {
        let inside = 1
        print(inside)
    }
    print(inside)
    

    The first print is fine. The last line reports cannot find 'inside' in scope, because inside only exists between the braces.

    How to fix one

    1. Read the name in the message exactly. The quoted word is what the compiler could not find.
    2. Compare it with the declaration, letter by letter and with attention to capitals.
    3. If the name should exist, check that it was declared and in a scope that reaches this line.
    4. Fix the spelling, or add the declaration, and compile again.

    Editors help here: autocomplete suggests existing names, so a typed suggestion cannot be misspelled.

    The exercise

    The exercise has a program that should print Welcome! and then sunny. Two names are wrong. Line 1 misspells the function, and line 4 misspells the variable declared on line 3:

    prnt("Welcome!")
    let weather: String = "sunny"
    print(whether)
    

    The compiler reports cannot find 'prnt' in scope and cannot find 'whether' in scope, one for each line. Fix the spelling of both:

    print("Welcome!")
    let weather: String = "sunny"
    print(weather)
    
    Welcome!
    sunny
    

    Notice how close weather and whether are. Real words that sound alike are an easy trap, so check the declaration when a variable is reported as missing.

    Key takeaways

    • An unknown name gives cannot find 'name' in scope.
    • The program is rejected before it runs, so nothing is printed.
    • Names are case-sensitive and must match their declarations exactly.
    • A name must be declared before use and be visible in the current scope.
    • Read the quoted name in the message and compare it with the declaration.
    Keep going with SwiftAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free