Swift : Unwrapping Like a Boss

Jay Mayu
Swift2Go
Published in
3 min readJul 18, 2018

In order to understand this article, you should have clear idea on Optionals. If you don’t know what is Optionals in Swift then please read my article on the same topic and come back here.

Simple Unwrapping

let name : String?  = "Jay"if let myName = name {   
print(myName)
}

As we can see above we can safely unwrap a optional variable using an if condition.

Unwrapping multiple Optionals

We know we can easily unwrap an optional using the if condition but the code will get messy if there are more than one optional that we need to unwrap at the given time. Look at the example below.

let name : String?  = "Jay"
let age : Int? = 45
let sex : String? = "Male"
if let myName = name {
print(myName)
if let myAge = age {
print(myAge)
if let mySex = sex {
print(mySex)
print("\(myName) is a \(mySex) and he is \(myAge) years old")
//this will return Jay is a Male and he is 45 years old
}
}
}

If you look at the code above it looks very messy with all the chaining. So, let’s approach it with an elegant way. Now look at the example below where the code is written in just single line.

let name : String?  = "Jay"
let age : Int? = 45
let sex : String? = "Male"
func singleLineUnwrap(){if let myName = name, let myAge = age, let mySex = sex {
print(myAge)
print(myName)
print(mySex)
print("\(myName) is a \(mySex) and he is \(myAge) years old")
}else{
print("something is missing")
}
}
singleLineUnwrap()

As you can see above, the three optional variables are unwrapped in a single line using the single if condition instead of chaining multiple if conditions. You may approach this way of unwrapping optionals to write more clean code.

Unwrap with Guard

The main difference between using a guard statement and if condition is that if condition’s block will be invoked if the optional has a value but guard statement’s block will be invoked if the optional has a nil value. I know it can be confusing when reading it. Don’t worry let’s look at the example below.

let name : String?  = "Jay"
let age : Int? = 45
let sex : String? = "Male"
func unwrap(){
guard let myName = name else {
print("Name not available")
return
}
guard let myAge = age else {
print("no age")
return
}
guard let mySex = sex else {
print("no sex")
return
}
print(myAge)
print(myName)
print(mySex)
print("\(myName) is a \(mySex) and he is \(myAge) years old")
}unwrap()

As you can see above when the unwrap() function is called, if one of the optional value is nil then the execution will exit and break out from the function due to the return statement. If all values are available then the print statements will be executed. I hope now you understand the difference between unwrapping with if condition and guard statement.

Now let’s look at a way to write more clean code to achieve what we did with guard statement in the previous example.

let name : String?  = "Jay"
let age : Int? = 45
let sex : String? = "Male"
func singleLineunwrapGuard(){
guard let myName = name, let myAge = age, let mySex = sex else {
print("someting is missing")
return
}
print("\(myName) is a \(mySex) and he is \(myAge) years old")
}

Now the above example looks more elegant as we wrote the guard statement in single line. Hope this article brought you some insight into unwrapping optionals in swift.

--

--