Jun 22 2020



HWS+ is an all new service from Hacking With Swift. As of writing, it has 3 courses, which each have a few sections. Until WWDC, HackingWithSwift will publish a new section to one of the 3 courses every day, which afterwards will be every few days.
I can say, after watching all the videos that are released, that they are from really high quality(not only the resolution, also the content🙂!), and are really recommended if you want to level up in your swift and iOS development skills.
Once you are there, be sure to check out the other courses they have here

Nil Coalescing in Swift: a way to unwrap optionals


We all are busy with it in our code: dealing with optionals. Optionals are around in swift since version 1, and have been very important to developers. Optionals in swift are a way for us to handle values that could possibly be nil. In this article I would like to show you one of the ways of dealing with posible optional values. I will not go into detail what optionals are, as that is one of the most complex topic in Swift

Single Nil Coalescing

As I said before, a lot of Swift developers have to deal with Optionals. One of the ways of dealing with optionals, is using Nil Coalescing. Nil Coalescing allows you to provide a default value that swift will use when the optional value is nil. Below is some code using nil coalescing:

let newScore = "766"
let score: Int = Int(newScore) ?? 0

What this code does is the following: First, I declared the variable newScore with a value of 766, as a string.

Second, I declared a variable named score . I want to assign the value of newScore to score. But, newScore has a data-type of string, and score has a data type of Int, and we cant assign a variable of one data type to a variable with another data type. If we need to assign a variable to another one, which both of them have different data types, we can, only if we convert it to that data type. Like in the example above, Int(newScore). But, this returns a option value, while score isn’t. That’s why we provide a default value using nil coalescing.

Using an optional as the default value for another optional

Sometimes, it could be useful to have 2 optional values for nil coalescing. Meaning, that the default value of the optional, is also an optional. You would do that by writing this:

let addedScore = "5"
let secondAddedScore = "10"
let score: Int = Int(addedScore) ?? Int(secondAddedScore)

What this code that is the following. First I have declared a variable called addedScore with a value of 5, as a string. Second ,I have declared another variable called secondAddedScore, with again the string value of 10. Then I have declared a third and final variable: score. Score is an int, so we can’t assign it the value of either addedScore or secondAddedScore. Thats why we needed to convert it to an Int. As it returns an int, we need to use nil coalescing to add a default value, as explained above. That being said, it could be that both of our variables that we want to use as that default value is optional, like in our case. In that case, swift allows us to use 2 optional values using nil coalescing for the score variable.

Wrap up

I hope I have learned you something new today, as I really enjoyed it to learn it my self, and then teach it to you. Did you like this topic and you would want to see more? Check out HWS plus here.