← iOS Development (Swift) learning pathiOS Development (Swift) · Module 3 of 10
Optionals and error handling
Handle missing values and errors safely.
📖 Key ideas
- An optional may contain a value or nil: var name: String?
- Unwrap safely with if let or guard let.
- ?? gives a default: name ?? "Guest".
- Force unwrap ! crashes if nil; avoid it.
- 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