Did you find this helpful? Why not support me to bring you even more content.
Click here to Buy Me a Coffee and say Thanks!

DISCLAIMER: This post is older than one year and maybe out of date.

Swift Nil Coalescing – Intermediate

How many times have you done some simple logic along these lines?

Well why not keep the code simple and stop your developer friends from teasing you by making these more efficient and impressive looking by using the Nil Coalescing Operator. You can represent the above with the simpler code below

So OK this is not a great example of code mastery but you get the idea, we can use the operator along with a ternary conditional and unwrapping to check a value then set a variable based on those results without having to use an if else block.

Not much more to explain on this one, just go have fun with it and thank me later for saving a few lines of code now and again!