Optional binding and chaining
Optional binding and chaining
When a mark exists, we want to use it normally; when it is absent, we want a clear alternative. Optional binding with if let creates a non-optional name inside the successful branch. Optional chaining asks for a member only if the optional value is present.
What you’ll learn
- Bind a present optional value within a branch.
- Trace optional chaining and its nil result.
Optional binding and chaining in the example
Check presence
When a mark exists, we want to use it normally; when it is absent, we want a clear alternative.
Check presenceBind a usable value
The bound value has a limited scope: it exists inside the branch, where its presence has been checked.
Bind a usable valueHandle absence explicitly
The score binding succeeds, so the actual constant contains 80 inside its branch.
Handle absence explicitly
How optional binding and chaining works
The bound value has a limited scope: it exists inside the branch, where its presence has been checked. Chaining with ?. skips the member access when the receiver is nil and returns an optional result. Neither operation invents data or guarantees a later independent access will be present.
Work through optional binding and chaining
Save this complete program as main.swift and run it with the Swift toolchain. No separate input file is needed.
Swift · complete program
let score: Int? = 80
if let actual = score {
print("Recorded:", actual)
} else {
print("Awaiting result")
}
let name: String? = "Diya"
print(name?.count ?? 0)Expected output
Recorded: 80
4Follow the execution
The score binding succeeds, so the actual constant contains 80 inside its branch. Optional chaining finds the name’s count, and the fallback is unused. If name were nil, no count lookup would occur and the final display would be zero under our chosen display policy.
Try this program in the Swift compiler. Paste the complete source above.
Practice: Optional binding and chaining
Your task: What happens to the message when score is nil?
Swift · complete program
let score: Int? = 80
if let actual = score {
print("Recorded:", actual)
} else {
print("Awaiting result")
}
let name: String? = "Diya"
print(name?.count ?? 0)Worked answer
The else branch prints Awaiting result. No non-optional actual value is created in that case, which avoids treating an absent score as a valid numeric result.
Use a value only after checking it
Nested optional access can produce absence because an earlier receiver or a later optional member is missing. Explain which absence is meaningful for your application. Use guard for early exits when the successful value should remain available later in a function.
Check your decision
Use a value only after checking it
The else branch prints Awaiting result. No non-optional actual value is created in that case, which avoids treating an absent score as a valid numeric result. Nested optional access can produce absence because an earlier receiver or a later optional member is missing.
Further reading: official Swift language guide.
Reaching the recap and completing the quizzes are separate milestones.