0Pricing
Swift Academy · Lesson

if let and Shorthand Binding

Unwrap optionals within a scope, including Swift 5.7 shorthand.

if let and Shorthand Binding 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.

Why Bind Optionals

An optional must be unwrapped before you can use the value inside. if let safely unwraps it only when a value exists.

let stored: Int? = 5
if let value = stored {
    print(value)
}

The if let Form

Write if let name = optional { ... }. Inside the braces, name is the unwrapped non-optional value. If the optional is nil, the block is skipped.

let input: String? = "Swift"
if let text = input {
    print(text.count)
}

Adding an else

You can attach an else to handle the nil case explicitly.

let age: Int? = nil
if let a = age {
    print("Age is \(a)")
} else {
    print("No age provided")
}

Shorthand Binding

Since Swift 5.7, if the new constant has the same name as the optional, you can write just if let value without the = value part.

let score: Int? = 90
if let score {
    print(score)
}

Shorthand vs Classic

The shorthand if let score is identical to if let score = score. It reduces repetition when names match.

let nickname: String? = "Sam"
if let nickname {
    print("Hi \(nickname)")
}

Scope of the Binding

The unwrapped constant only exists inside the if body. Outside, the optional is still optional.

let maybe: Int? = 7
if let maybe {
    print(maybe + 1)
}
print(maybe as Any)

Binding From Conversions

if let pairs naturally with failable conversions like Int(string).

let raw = "42"
if let number = Int(raw) {
    print(number * 2)
} else {
    print("Not a number")
}

Binding Dictionary Values

Use if let to safely read a dictionary value that may be missing.

let ages = ["Ann": 30]
if let annAge = ages["Ann"] {
    print(annAge)
}
if let bobAge = ages["Bob"] {
    print(bobAge)
} else {
    print("Bob not found")
}

Binding a Mutable Copy

Use if var when you want to modify the unwrapped value locally. The change does not affect the original optional.

let count: Int? = 10
if var c = count {
    c += 5
    print(c)
}

Nesting Avoided

You can put logic that depends on the value safely inside the binding block, avoiding force-unwrapping with !.

let name: String? = "Lia"
if let name {
    let upper = name.uppercased()
    print(upper)
}

Why Not Force Unwrap

Force unwrapping with ! crashes if the optional is nil. if let is the safe alternative that never crashes.

let value: Int? = nil
if let value {
    print(value)
} else {
    print("Safe: was nil")
}

Quick Check

Test optional binding.

Recap: if let and Shorthand Binding

if let safely unwraps an optional into a local constant only when a value exists. The Swift 5.7 shorthand if let name avoids repeating the name. Prefer it over force-unwrapping.

let x: Int? = 3
if let x { print(x) }

Frequently asked questions

Is the “if let and Shorthand Binding” lesson free?

Yes — the full text of “if let and Shorthand Binding” 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 “if let and Shorthand Binding”?

Unwrap optionals within a scope, including Swift 5.7 shorthand. 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 “if let and Shorthand Binding” 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. if let and Shorthand Binding
  2. guard let for Early Exit
  3. Binding Multiple Optionals
  4. Optional Pattern in switch and for
← Back to Swift Academy