What Is Nil Coalescing Operator In Swift
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 options to ensure optional 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 –
let username: String? = nil
let unwrappedName = username ?? “defaultUserName”
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 you should know?
Nil coalescing operators are completely cleaned up, easy-to-read, easy-to-understand, it can shorthand the longer code that developer 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
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.
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 contact our experts.
I hope this will helps you. happy coding! 👍