DevPrep

All topics › The Swift Language

What is an optional and what problems does it solve?

Junior PRO

The idea

Optional<Wrapped> is an enum with two cases: .none and .some(Wrapped). String? is sugar for Optional<String>, and nil is a literal for .none. String and String? are different types: the compiler will not let you use an optional where a value is required until you unwrap it.

The rest of this answer, with code and interview follow-ups, is in the DevPrep app.

Read the full answer in DevPrep

Practice this before your interview

DevPrep turns these answers into one-minute challenges: what does this code print, find the bug, mock interviews. One challenge a day.

Get DevPrep on the App Store