0Pricing
Swift Academy · Lesson

Optional chaining & error handling basics

Use optional chaining, guard patterns, try?/try!, and do/catch to handle absence and errors. Learn Result as an alternative.

Optional chaining & error handling basics is a free Swift Academy lesson on CoddyKit — lesson 1 of 3. 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 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Intro

This lesson links Optionals with error handling. Explore chaining, guard patterns, try?/try!, and Result.

Optional chaining

Optional chaining (a?.b?.c) stops and returns nil if any link is nil.

struct User { var address: Address? }
struct Address { var city: String }
let u: User? = User(address: Address(city: "Paris"))
print(u?.address?.city)   // Optional("Paris")

guard pattern

guard let exits early if an optional is nil, keeping unwrapped value after.

func greet(_ name: String?) {
    guard let n = name else {
        print("No name")
        return
    }
    print("Hello, \\(n)!")
}
greet(nil)
greet("Ada")

try? vs try!

try? returns nil on error. try! crashes if an error is thrown.

enum ParseErr: Error { case bad }
func parse(_ s: String) throws -> Int {
    guard let n = Int(s) else { throw ParseErr.bad }
    return n
}
let ok = try? parse("10")   // Int?
let fail = try? parse("xx") // nil
// let crash = try! parse("xx")  // runtime crash

do/catch

do/catch handles thrown errors explicitly.

do {
    let n = try parse("42")
    print(n)
} catch {
    print("error:", error)
}

Result type

Result<T,Error> encodes success/failure without throwing.

func safeParse(_ s: String) -> Result<Int,Error> {
    do { return .success(try parse(s)) }
    catch { return .failure(error) }
}
print(safeParse("5"))
print(safeParse("oops"))

Optional chaining Q

Quick check: What does optional chaining do?

Recap

Recap: Combine optionals and error handling. Use chaining, guard, try?/try!, do/catch, and Result to manage absence and failures safely.

Frequently asked questions

Is the “Optional chaining & error handling basics” lesson free?

Yes — the full text of “Optional chaining & error handling basics” is free to read here on the web, and the Swift Academy course includes 3 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 “Optional chaining & error handling basics”?

Use optional chaining, guard patterns, try?/try!, and do/catch to handle absence and errors. Learn Result as an alternative. 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 3, so you can start here or from the beginning and move at your own pace.

How long does the “Optional chaining & error handling basics” 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. Optional chaining & error handling basics
  2. do/catch & defining errors (enums)
  3. Result types — Result
← Back to Swift Academy