The Nil-Coalescing Operator
Supply a default when an optional is nil.
The Nil-Coalescing Operator is a free Swift Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
The Problem of nil
An optional may hold a value or be nil. Often you want to use the value if present, or fall back to a default if it is nil.
let typedName: String? = nil
let name = typedName ?? "Guest"
print(name)The ?? Operator
The nil coalescing operator ?? takes an optional on the left and a default on the right. If the optional is non-nil, you get its unwrapped value; otherwise you get the default.
let stored: Int? = 42
let value = stored ?? 0
print(value)Default Is Used Only on nil
The right-hand default is only used when the left side is nil. A present value, even 0 or an empty string, is kept.
let count: Int? = 0
let result = count ?? 99
print(result)Result Is Non-Optional
The expression optional ?? default produces a non-optional value, because there is always something to fall back to.
let nickname: String? = nil
let display: String = nickname ?? "Anonymous"
print(display)Replacing if let
Without ?? you would write an if/else to unwrap and provide a default. Nil coalescing collapses that into one expression.
let age: Int? = nil
var finalAge: Int
if let unwrapped = age {
finalAge = unwrapped
} else {
finalAge = 18
}
print(finalAge)Same Logic With ??
Here is the previous example rewritten with nil coalescing. It is shorter and the intent is obvious.
let age: Int? = nil
let finalAge = age ?? 18
print(finalAge)Coalescing in Interpolation
You can drop ?? directly inside string interpolation to print a friendly fallback.
let city: String? = nil
print("City: \(city ?? "Unknown")")From Failable Conversions
Converting a String to an Int returns an optional. Nil coalescing gives a default when the text is not a number.
let text = "abc"
let number = Int(text) ?? -1
print(number)Coalescing With Dictionaries
Looking up a key in a dictionary returns an optional. Use ?? to supply a fallback when the key is missing.
let scores = ["Ann": 90]
let bob = scores["Bob"] ?? 0
print(bob)The Default Can Be Computed
The right side can be any expression of the right type, even a function call. It is only evaluated when needed.
func defaultName() -> String {
return "Player1"
}
let entered: String? = nil
print(entered ?? defaultName())Type Must Match
The default must be compatible with the optional wrapped type. An Int? needs an Int default.
let maybe: Int? = nil
let safe = maybe ?? 100
print(safe + 1)Quick Check
Test nil coalescing.
Recap: The Nil Coalescing Operator
The ?? operator unwraps an optional or falls back to a default, producing a non-optional result. The default is only used when the optional is nil.
let input: String? = nil
let name = input ?? "Default"
print(name)Frequently asked questions
Is the “The Nil-Coalescing Operator” lesson free?
Yes — the full text of “The Nil-Coalescing Operator” is free to read here on the web, and the Swift Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.
What will I learn in “The Nil-Coalescing Operator”?
Supply a default when an optional is nil. 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; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “The Nil-Coalescing Operator” 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
- The Nil-Coalescing Operator
- Chaining Default Values
- Defaults in Function Parameters
- Dictionary Default Subscripts