What is nil coalescing operator in swift

Ashvini Wable
Nov 7 · 3 min read

Many times, we have to face situations where we need to be ready with our plan B for such things where we already know that plan A can lead to failure. This thing is also applicable while development and nil coalescing operator — ?? — helps us to do this.

  • Optional is an authoritative source provided by swift, but while unwrapping their values, we could fall into app crashes/breaking our code flow if optional, don’t have a value in it.
  • So for avoiding the above problem and safely unwrapping optional to ensure it has a meaningful value, nil coalescing operator — ?? — is used.

Today, in this post, I would like to highlight the use of “??” in swift with a code snippet. So, let’s take a closer look:

Work Processes of the nil coalescing operator

If the optional has a value, in that case, the nil coalescing operator can unwrap it. If the value of an optional is nil, it can assign it as a default value instead.

What is nil coalescing operator-??- in swift?

Swift’s nil coalescing operator — ?? — unwraps the optional if it has a value or provides a default value if it is empty. Let’s see the below example -

Here, the username is an optional variable, and we need to unwrap it safely. The nil coalescing operator exactly does that. If the username variable contains a value, then it will assign that value to unwrappedName variable; otherwise, the default value will be assigned to unwrappedName. Does it mean unwrappedName has the data type String always rather than String? Coz it can be guaranteed to have an exact value.

Why is nil coalescing operator-?? — so important?

You should use this operator to unwrap options guaranteed to have a value in it safely. It will avoid your app crashes as well as code flow breaks because of dependencies on optional benefits.

Tips for using nil coalescing operator-??

You can also write it in a single line as:

print(“Welcome \(username ?? “default username”)!”)

What else should you know?

Nil coalescing operators are completely cleaned up, easy-to-read, easy-to-understand, it can shorthand the longer code that developers use in the ternary conditional and forced unwrapping. Let’s check the longhand version example that Apple gives:

a != nil ? a!: b

With the nil coalescing operator, this becomes:

a ?? b

Much cleaner.

When you can use it in your project?

I have found nil coalescing is extremely helpful when instantiating the objects. Sometimes, it is a terrific way to ensure that safely your code will run regardless of any possible problems with an API. Or you can say your data comes back in nil or an unexpected format. In that case, if needed Simply give your object a default value and you are good to go.

Of course, only use nil coalescing if the possibility of assigning a self-generated default value to your object makes sense — it might not. But when it does, it’s very useful!

What are the options if I do not want to use ??

Where should I not use ??
If your variables don’t have an optional data type then no need to use nil coalescing operator.

Conclusion

This article is all about the advantages of nil coalescing operator. Here I mentioned where and how you can implement it in your project. If you have any farther queries don’t feel hesitate to post comments.

I hope this will helps you. happy coding! 👍

Ashvini Wable

Written by

iOS developer

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade