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.

Lesson 1 of 38 steps

Optional chaining & error handling basics is a free Swift Academy lesson on CoddyKit. This is lesson 1 of 3. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, and your progress syncs across the web and the CoddyKit app. The Swift Academy course includes 3 lessons in total.

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.

Free to start

Learn Swift with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
122
Lessons
409

Frequently Asked Questions

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

Yes — any 3 lessons of the Swift Academy learning path, including “Optional chaining & error handling basics”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Swift Academy course includes 3 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 3.

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