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.
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:
means:
- if
optionalValuecontains a value, use it - otherwise, use
defaultValue
Simple example:
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:
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 ?? fallbacksays “If it is nil, use this fallback safely.”'
Example:
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.
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 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
- What's the UIScrollView contentInset property for?
- What's toolscontext in Android layout files?
- What's toolscontext in Android layout files?
- When does SQLiteOpenHelper onCreate / onUpgrade run?
- When is it better to use an NSSet over an NSArray?
- When is layoutSubviews called?
- When should I release objects in -voidviewDidUnload rather than in -dealloc?
- When should one use RxJava Observable and when simple Callback on Android?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.