The rules the compiler enforces
A Swift name can contain letters, digits and underscores, and it cannot start with a digit. Spaces and symbols such as . or # are not allowed. Break a rule and the code does not compile:
let 18spam: Int = 1
error: 's' is not a valid digit in integer literal
error: expected pattern
Swift reads the leading 18 as the start of a number, so it never sees a name. A space splits one name into two, which is also an error:
let first name = "Kate"
error: found an unexpected second identifier in constant declaration; is there an accidental break?
Names are also case-sensitive, so name, Name and NAME are three different constants:
let name = "Kate"
let Name = "Andy"
let NAME = "George"
print(name, Name, NAME)
Kate Andy George
That is legal but confusing, and a bad habit. Tell names apart by their meaning, not their capitals.
Conventions for multi-word names
Because spaces are not allowed, a name made of several words needs another way to show where one word ends. Two styles are common:
- camelCase: the first word is lowercase and each following word starts with a capital, as in
firstNameandnumberOfEmployees. - snake_case: words are separated by underscores, as in
first_name.
Both compile. Swift code, including its own standard library, uses camelCase, and this lesson does too. Pick one style and stay with it. A program that mixes first_name and lastName runs, but it is harder to read.
Booleans are usually named so they read like a yes/no question: isEmployed, hasLicense.
Names should describe the data
The compiler accepts a, b and c just as happily as descriptive names. A reader does not:
let hours = 25
let rate = 13
let pay = hours * rate
print(pay)
325
With hours, rate and pay, the calculation explains itself. Short names such as x and i are fine for tiny scopes, but use a descriptive name for anything that matters.
The exercise, step by step
- Declare
firstNamewithletand give it the string"John". The two words are joined with a capitalN. - Declare
isEmployedwithletand the valuetrue. Theisprefix signals aBool. - Declare
yearsOfExperiencewithletand the value5, in the same camelCase style.
The solution in Swift
let firstName = "John"
let isEmployed = true
let yearsOfExperience = 5
Details to know:
- Digits are fine after the first character.
user2is valid, and2useris not. - Reserved words need backticks. Keywords like
classcannot be names by themselves. Wrapping one in backticks, as inlet `class` = "x", works, but choosing another name is simpler. - Swift allows many Unicode letters in names, for example
café, but plain ASCII keeps code easy to type. - Types are capitalised. By convention
Int,Stringand your own types start with a capital letter, while constants and variables start lowercase. That is one more reason to avoidNameas a variable.
Common mistakes
- Starting with a digit, as in
18spam. - Putting a space in a name, as in
first name. - Changing the case when you use a name. Writing
firstnamewhen the constant isfirstNamegives a "cannot find in scope" error. - Mixing camelCase and snake_case in one program.
Key takeaways
- Use letters, digits and underscores, and never start with a digit.
- Names are case-sensitive.
- Write multi-word names in camelCase and stay consistent.
- Choose names that tell the reader what the value means.
