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 anifor a function, is not visible after the closing brace. - Forgetting a declaration. The code uses
total, but the linevar total = 0was 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
- Read the name in the message exactly. The quoted word is what the compiler could not find.
- Compare it with the declaration, letter by letter and with attention to capitals.
- If the name should exist, check that it was declared and in a scope that reaches this line.
- 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.
