0Pricing
Swift Academy · Lesson

Chaining Default Values

Combine multiple fallbacks cleanly.

Chaining Default Values is a free Swift Academy lesson on CoddyKit — lesson 2 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.

More Than One Fallback

Sometimes you have several optional sources and want the first non-nil one. You can chain multiple ?? operators together.

let primary: String? = nil
let secondary: String? = "Backup"
let name = primary ?? secondary ?? "Default"
print(name)

How Chaining Reads

Read a ?? b ?? c as: use a if it is non-nil, otherwise b, otherwise c. Evaluation stops at the first value found.

let a: Int? = nil
let b: Int? = nil
let c = 7
print(a ?? b ?? c)

First Non-nil Wins

As soon as an option provides a value, the rest are ignored. Here secondary wins and the final default is never reached.

let primary: Int? = nil
let secondary: Int? = 50
let fallback = 0
print(primary ?? secondary ?? fallback)

All Optional Until the Last

In a chain, every operand except the final one is typically an optional. The last one is the guaranteed non-optional default.

let nick: String? = nil
let full: String? = nil
let result: String = nick ?? full ?? "Anonymous"
print(result)

Short-Circuit Evaluation

Like && and ||, the ?? chain short-circuits. Once a value is found, later expressions are not evaluated.

func makeDefault() -> Int {
    print("default computed")
    return 0
}
let found: Int? = 9
print(found ?? makeDefault())

Right Associativity

The ?? operator is right-associative, so a ?? b ?? c groups as a ?? (b ?? c). This is exactly the behavior you want for fallbacks.

let x: Int? = nil
let y: Int? = nil
let z: Int? = 3
print(x ?? (y ?? (z ?? 0)))

Layered Config Defaults

Chaining shines for layered configuration: user setting, then app setting, then a hard-coded default.

let userTheme: String? = nil
let appTheme: String? = "light"
let theme = userTheme ?? appTheme ?? "system"
print(theme)

Combining With Conversions

You can chain failable conversions with a final default to robustly parse input.

let raw: String? = nil
let parsed = raw.flatMap { Int($0) } ?? 0
print(parsed)

Dictionary Lookups in a Chain

Several dictionary lookups can be chained so the first available key is used.

let prefs = ["color": "blue"]
let pick = prefs["accent"] ?? prefs["color"] ?? "gray"
print(pick)

Keeping Chains Short

Two or three fallbacks read fine. If you find yourself chaining many, consider extracting the logic into a function for clarity.

let one: String? = nil
let two: String? = nil
let three = "C"
print(one ?? two ?? three)

Result Type of a Chain

The final result of a chain is non-optional as long as the last operand is non-optional. This makes the value safe to use immediately.

let v1: Int? = nil
let v2: Int? = 4
let total: Int = (v1 ?? v2 ?? 0) + 1
print(total)

Quick Check

Test fallback chains.

Recap: Chaining Default Values

Chain ?? operators to try several optionals in order and fall back to a final default. The first non-nil value wins, evaluation short-circuits, and the result is non-optional.

let p: String? = nil
let s: String? = "ok"
print(p ?? s ?? "none")

Frequently asked questions

Is the “Chaining Default Values” lesson free?

Yes — the full text of “Chaining Default Values” 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 “Chaining Default Values”?

Combine multiple fallbacks cleanly. 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 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Chaining Default Values” 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

  1. The Nil-Coalescing Operator
  2. Chaining Default Values
  3. Defaults in Function Parameters
  4. Dictionary Default Subscripts
← Back to Swift Academy