Optionals & nil Safety

Optionals are Swift's signature safety feature. By the end of this lesson you'll declare optionals with ? , understand what nil means, and unwrap values safely with if let , guard let , and ?? — while knowing exactly why force-unwrapping with ! is risky.

Learn Optionals & nil Safety in our free Swift course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.

Part of the free Swift course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

What You'll Learn in This Lesson

1️⃣ Declaring Optionals & What nil Means

Add ? to any type to make it optional. An optional holds either a wrapped value or nil . The shorthand Int? is identical to Optional<Int> . Optionals show up naturally wherever a value might be missing — like converting text to a number, which can fail.

2️⃣ Safe Unwrapping: if let & guard let

You can't use an optional directly — you must unwrap it. if let runs a block only when the value is present, binding a plain non-optional inside. guard let unwraps too, but its else must exit the current scope, keeping the rest of your function flat and readable.

3️⃣ Defaults with ?? and the Danger of !

The nil-coalescing operator ?? gives you a clean fallback: value ?? default . Force-unwrapping with ! skips every check and crashes if the optional is nil — so reserve it for cases where you are 100% certain a value exists.

Your turn. Fill in the two blanks — one with ?? , one with if let — then run it and check the output.

📋 Quick Reference

Build it, run it, and check your output. Handling "maybe missing" values cleanly is something you'll do in almost every real Swift program.

Practice quiz

What does an optional type like String? represent?

  • A String that is always empty
  • Either a String value or nil (no value)
  • A faster kind of String
  • A String that cannot be changed

Answer: Either a String value or nil (no value). An optional holds either a wrapped value or nil.

What is the full, non-shorthand spelling of Int??

  • Optional<Int>
  • Maybe<Int>
  • Int.optional
  • Nullable<Int>

Answer: Optional<Int>. Int? is syntactic sugar for Optional<Int>.

What does mean in Swift?

  • The number zero
  • An empty string
  • The absence of a value
  • A runtime error

Answer: The absence of a value. nil means there is no value present.

What happens if you force-unwrap a nil optional with ?

  • You get nil back
  • You get a default value
  • The program crashes at runtime
  • It returns 0

Answer: The program crashes at runtime. Force-unwrapping nil triggers a fatal runtime crash.

Which is the SAFE way to unwrap and use an optional?

  • Force-unwrap with !
  • Optional binding with
  • Ignore it
  • Add ? to every use

Answer: Optional binding with. if let safely binds the value only when it is non-nil.

What does the nil-coalescing operator do?

  • Force-unwraps the optional
  • Provides a default value when the optional is nil
  • Converts to a String
  • Checks equality

Answer: Provides a default value when the optional is nil. a ?? b returns a's value if non-nil, otherwise b.

Can a non-optional Swift variable hold nil?

  • Yes, any variable can
  • No, only optionals can hold nil
  • Only Strings can
  • Only with a special flag

Answer: No, only optionals can hold nil. Only optional types may hold nil; non-optionals always have a value.

What is the type of ?

  • Int
  • Int? (it returns nil here)
  • String
  • Bool

Answer: Int? (it returns nil here). Int(String) is a failable initializer returning Int?, which is nil for non-numeric text.

After , what is the type of inside the braces?

  • String?
  • Optional
  • String (unwrapped, non-optional)
  • nil

Answer: String (unwrapped, non-optional). Optional binding gives you the unwrapped, non-optional value inside the block.

Which keyword combination unwraps optionals early in a function, exiting if nil?

  • if let
  • guard let
  • while let
  • switch let

Answer: guard let. guard let unwraps and requires an else that exits the current scope on nil.