Swift
double question mark
Swift programming
optional unwrapping
Swift language features

what's the purpose of double question mark in swift

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

In Swift, ?? is the nil-coalescing operator. Its job is to unwrap an optional if a value exists and otherwise return a fallback value, which makes optional handling shorter and clearer than many equivalent if let or ternary expressions.

What ?? Means

The expression:

swift
optionalValue ?? defaultValue

means:

  • if optionalValue contains a value, use it
  • otherwise, use defaultValue

Simple example:

swift
1let nickname: String? = nil
2let displayName = nickname ?? "Anonymous"
3
4print(displayName)  // Anonymous

If nickname were non-nil, displayName would receive that actual value instead of the fallback.

Why It Exists

Swift optionals force you to acknowledge that a value may be missing. ?? exists to cover one of the most common patterns: “use this optional if present, otherwise fall back to a sensible default.”

Without ??, the same idea is more verbose:

swift
1let displayName: String
2if let nickname = nickname {
3    displayName = nickname
4} else {
5    displayName = "Anonymous"
6}

The nil-coalescing operator expresses that logic in one line without losing clarity.

It Is Not the Same as Force Unwrapping

A common confusion is mixing ?? with !.

  • 'value! says “I am certain this optional is not nil.” If you are wrong, the app crashes.'
  • 'value ?? fallback says “If it is nil, use this fallback safely.”'

Example:

swift
let ageText: String? = nil
let safeText = ageText ?? "Unknown"

This is usually a better default than force unwrapping when missing values are acceptable.

It Short-Circuits

The right-hand side is evaluated only when the optional is nil. That means expensive fallback work is skipped when the optional already contains a value.

swift
1func makeDefaultName() -> String {
2    print("Computing fallback")
3    return "Anonymous"
4}
5
6let name: String? = "Taylor"
7let result = name ?? makeDefaultName()
8
9print(result)

In this case, makeDefaultName() is not called because name already holds a value.

Chaining Fallbacks

You can chain nil-coalescing expressions when there are several possible sources:

swift
let preferredName: String? = nil
let username: String? = nil
let finalName = preferredName ?? username ?? "Guest"

Swift evaluates those from left to right until it finds the first non-nil value.

This is useful in configuration resolution, display-name selection, and optional field mapping.

Use It When the Fallback Is Meaningful

?? works best when the default value is genuinely correct for the situation. For example, a UI label may reasonably fall back to “Unknown,” but a missing API token should probably not be silently replaced with an empty string.

In those cases, guard let or explicit error handling may communicate intent better.

Common Pitfalls

The most common mistake is using ?? to hide missing data that should be treated as an error. A fallback can make bad state look valid.

Another issue is forgetting operator precedence when combining ?? with other expressions. Parentheses help when the expression becomes hard to read.

People also overuse it in situations where optional binding would be clearer, especially if more logic is needed than simply choosing a default value.

Finally, do not confuse ?? with the ternary operator. Nil-coalescing is specifically for optionals, not for arbitrary boolean conditions.

Summary

  • '?? is Swift’s nil-coalescing operator.'
  • It unwraps an optional when possible and otherwise returns a fallback value.
  • It is safer than force unwrapping when nil is acceptable.
  • The fallback expression is evaluated only if the optional is nil.
  • Use it when a real default value makes sense, not to hide meaningful errors.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.