Optionals intro: ?, nil, if let, guard let, ??
Introduce optionals: declaring with ?, representing missing values with nil, and unwrapping via if let, guard let, and ??
Optionals intro: ?, nil, if let, guard let, ?? is a free Swift Academy lesson on CoddyKit. This is lesson 3 of 3. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, and your progress syncs across the web and the CoddyKit app. The Swift Academy course includes 3 lessons in total.
What is an Optional?
Optionals represent a value that may be present or nil. Add ? to a type to make it optional.
- Handle absence explicitly
- Avoids many crashes
if let binding
Declare with ?, then use if let to unwrap safely when present.
var nickname: String? = nil
print("nickname =", String(describing: nickname))
// Optional binding with if let
let input: String? = "Ada"
if let name = input {
print("Hello, \\(name)")
} else {
print("No name")
}guard let
guard let unwraps and exits early on nil, keeping code flat and readable.
func greet(_ maybeName: String?) {
guard let name = maybeName else {
print("Missing name"); return
}
print("Hi, \\(name)!")
}
greet(nil)
greet("Grace")Nil-coalescing (??)
Use ?? to supply a default when the optional is nil.
let provided: String? = nil
let username = provided ?? "Guest"
print("Username:", username)Optional chaining
Optional chaining (?.) stops the chain when any link is nil.
struct Profile { var city: String? }
struct User { var profile: Profile? }
let u1 = User(profile: Profile(city: "Paris"))
let u2 = User(profile: nil)
// Optional chaining safely traverses missing links
print(u1.profile?.city?.uppercased() ?? "No city") // "PARIS"
print(u2.profile?.city ?? "No city") // "No city"Tips & pitfalls
Tips:
- Avoid
!(force unwrap) unless you are 100% sure it is not nil. - Prefer if let or guard let for clarity.
- Use ?? to provide safe defaults.
guard let behavior
Quick check: What does guard let do?
Recap
Recap: Declare optionals with ?, represent absence with nil, unwrap using if let or guard let, and provide defaults with ??. Use optional chaining to traverse safely.
Learn Swift with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 122
- Lessons
- 409
Frequently Asked Questions
Is the “Optionals intro: ?, nil, if let, guard let, ??” lesson free?
Yes — any 3 lessons of the Swift Academy learning path, including “Optionals intro: ?, nil, if let, guard let, ??”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Swift Academy course includes 3 lessons in total.
What will I learn in “Optionals intro: ?, nil, if let, guard let, ??”?
Introduce optionals: declaring with ?, representing missing values with nil, and unwrapping via if let, guard let, and ?? You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Swift Academy?
No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 3.
How long does the “Optionals intro: ?, nil, if let, guard let, ??” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Swift Academy lesson?
Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- let vs var, type inference, literals, tuples
- Core types: numbers, Bool, String, Character
- Optionals intro: ?, nil, if let, guard let, ??