← iOS Development (Swift) learning path
iOS Development (Swift) · Module 3 of 10

Optionals and error handling

Handle missing values and errors safely.

📖 Key ideas

  1. An optional may contain a value or nil: var name: String?
  2. Unwrap safely with if let or guard let.
  3. ?? gives a default: name ?? "Guest".
  4. Force unwrap ! crashes if nil; avoid it.
  5. Errors: throws, try, do-catch.
💡 Real-life example: An optional is a gift box that might be empty; open it carefully before using what is inside.
OptionalValue or nil
if letSafe unwrapping
guard letEarly exit unwrapping
do-catchHandles thrown errors