Codementor Events

Optionals in Swift

Published Jan 15, 2018Last updated Jul 14, 2018


https://pixabay.com/en/question-question-mark-survey-2736480/

Absent of Data

There are situations where we wish to accept data from users and also provide an opportunity for some of the data to be optionals, meaning they are not required but should be provided if available.

Person {
  var firstname: String
  var middlename: String
  var lastname: String
}

//this will definitely work because i have a middlename
let me = Person(firstname: "Abel", middlename: "abel", lastname: "Adeyemi")

//This will throw an error
let anotherPerson = Person(firstname: "Abel", middlename: nil, lastname: "Adeyemi")

The constant me instance of a person provided all the data but the constant anotherPerson cannot provide all the data because he probably does not have a middlename. Hence he passed nil as the value of the middlename. Sounds cool right but the nil passed will cause an error, Why?, because we have not told swift to make the middlename optional.

What is an Optional in Swift?

An optional in Swift is a type that can hold either a value or no value. Optionals are written by appending a ?to any type:

var middlename: String?

The above means that middlename can either be a string or does not contain anything which is represented by the ?. An optional is a kind of container. An optional String is a container which might contain a string. An optional Int is a container which might contain an Int. Think of an optional as a kind of parcel. Before you open it (or “unwrap” in the language of optionals) you won’t know if it contains something or nothing. Its only an Optional value that can be set to nil, and that was the reason for the error when we passed the middlename a value of nil without specifying its a type of Optional.

var red: String = "Red"
red = nil // error: nil cannot be assigned to type 'String'

How to create an Optional?

The simplest way to create an optional in swift is to add ? in front of the type like below

var middlename: String?

Another way is using the Optional keyword like below

var middlename: Optional<String>

Using (unwraping) an optional

The simplest way to unwrap an optional is to add a !after the optional name.

Person {
  var firstname: String
  var middlename: String? //we created the optional with the ?
  var lastname: String
  
  func fullName() -> () {
     print("Your fullname is \(firstname) \(middlename!) \(lastname)")
  }
}

let me = Person(firstname: "Abel", middlename: "Agoi", lastname: "Adeyemi")

me.fullName() //will return "Your fullname is Abel Agoi Adeyemi"

Notice the \(middlename!), the ! there is use to unwrap the middlename so we can get the value “abel”. The problem is that when we passed a nil value to the middlename, we will get a runtime crash which says nil is Unexpected.

Person {
  var firstname: String
  var middlename: String?
  var lastname: String
  
  func fullName() -> () {
     print("Your fullname is \(firstname) \(middlename!) \(lastname)")
  }
}

let anotherPerson = Person(firstname: "Abel", middlename: nil, lastname: "Adeyemi")

anotherPerson.fullName() //This will throw a runtime error

Before unwrapping, we need to be sure that there’s a value. So therefore we are going to continue the rest of the article with how to check before using (unwrapping) optionals.

Use If to Check for nil before force unwrapping:

Since it was nil that was passed to the variable when we initialized the Person struct, we can use an if statement to check for nil before unwrapping the Person struct like below

Person {
  var firstname: String, var middlename: String?, var lastname: String
  
  func fullName() -> () {
     if (middlename != nil) {
        print("Your fullname is \(firstname) \(middlename!) \(lastname)")  
     } else {
        print("Your fullname is \(firstname) \(lastname)")
     }
     
  }
}

let anotherPerson = Person(firstname: "Abel", middlename: nil, lastname: "Adeyemi")

anotherPerson.fullName() //This will not throw an error

Whenever we use the ! symbol, it is denoted as force unwrapping the optional value, but it a common practice to avoid force unwrapping it, because we can write our code in such a way that swift will automatically unwrap it for us

Using Optional binding:

With optional binding, though we are still using if, swift provide a way for us to use the if let statement then automatically unwrap it for us like below

Person {
  var firstname: String, var middlename: String?, var lastname: String
  
  func fullName() -> () {
    
     if let middlename: String = middlename {
         print("Your fullname is \(firstname) \(middlename) \(lastname)")
     } else {
         print("Your fullname is \(firstname) \(lastname)")
     }
    
  }
}

let anotherPerson = Person(firstname: "Abel", middlename: nil, lastname: "Adeyemi")

anotherPerson.fullName() //This will not throw an error

Using Guard for early exit:

Guard provide a way to quickly exit when checking for the optional string and make our code much more cleaner hence there wont be need for if else statement.

Person {
  var firstname: String, var middlename: String?, var lastname: String
  
  func fullName() -> () {
    
     guard let middlename = middlename else {
       print("Your fullname is \(firstname) \(lastname)")  
       return
     }
    
     print("Your fullname is \(firstname) \(middlename) \(lastname)")
    
  }
}

let anotherPerson = Person(firstname: "Abel", middlename: nil, lastname: "Adeyemi")

anotherPerson.fullName() //This will not throw an error

We have covered how to use optionals in swift, let us look at some cool secondary use cases of optionals which include using multiply optionals, method chaining and Nil Coalescing.

class Street {
    var streetName: String?
}
class House {
    var noOfRooms = 1
    var street: Street?
}
class Person {
    var house: House?
}

let street = Street()
street.streetName = "Adeyeye"

let house = House()
house.street = street

let me = Person()
me.house = house


The streetName in the Street class is an optional string, the House class has a street variable which is also a Optional Street. The Person class too has a variable house which is an Optional house. When we have to access the person street. To access the person street (myStreet), we ain’t sure if the person gave us the house information (myHouse). That was the reason why we use the if let myHouse = Person.house. If the Person.houseis empty, the if statement will fail to execute, else the variable will be assigned to the myHouse property. Next, we have to access the myHouse.street and if its available, the variable will be assigned to the myStreet.

if let myHouse = Person.house, let myStreet = myHouse.street {
      print(myStreet) //this will access the street
}

The pattern used to access the myStreet can be improved with the use of optional chaining like below:


//if the Person.house fails, it wil not get to the street
//which can either return a null or the street

let myStreet = Person.house?.street

//to make sure we get a value, we can include the if let like below
if let myStreet = Person.house?.street {
  print(myStreet)
}

I am happy to share this article with you. If you’ve enjoyed this article, do show support by giving a few claps 👏_ . Thanks for your time and make sure to follow me or drop your comment below _👇

Discover and read more posts from Abel Agoi
get started
post commentsBe the first to share your opinion
Show more replies