Optionals In Swift Programming Language


In this article, we will learn the concept of Optionals in Swift language and implement some examples with the coding. I will try to be clear and give a better understanding of the Optionals topic.

The Difficulty Before Optionals

First, we make a simple program in Xcode editor by declaring the variable integer and naming it temperature. In the second line, we print this temperature. But, when we print this integer, the error message appears that you must initialize the temperature.


Now, I am changing the code and assigning zero to the temperature variable. Our code works fine after this change. Now, suppose you are connected with the internet and the temperature is updated, it prints the variable and we are able to see the temperature now. If our internet is not connected, then also it shows the temperature but with one problem; Zero is a valid temperature and it means that the day is winter and freezing. So, how to solve this problem when the internet is not connected?


What are Optionals?

  • Optional is a special Swift type that can represent not just the value but also the absence of the value.
  • Nil represents the absence of the value.
  • Optionals also work variably.

Overcome Temperature Problem Using Optionals

Now, considering the previously discussed example, we put the “?” after an integer. That means when there is no value or the absence of the value, it displays nil. It is the best approach when you want to see nothing. Your user will see the nil message. Now, if the internet is not connected the nil will be displayed otherwise the temperature will be shown.


Optional Phrase Problem

When we use “?” with any data type or anything, it prints the 'Optional' phrase with those words where we use the “?”sign. It tells the users and the developer that this word is optional. Thus, we need to overcome this problem, too.


Force Unwrapping

The force unwrapping concept is nothing. We assign the “!” sign to overcome the Optional phrase. Afterwards, when we run the code, it prints fine and it means that we take an integer as a positive value.


Optional Binding

In Swift, a feature of Optional binding is included. In Optional Binding, you don’t need to unwrap the value and you can easily access the value inside an Optional. The example below will clearly explain the concept of Optional Binding.

Here, we declare the dictionary name as a state and input some key values in it. We make a new variable named as result. When we give the state name, it checks whether the state is present in the dictionary or not. Since it is not present in the dictionary, the “var result” is treated here as optional and gives you the result nil. Well! This is called Optional Binding.


Main Points

  • Optional represents the nil concept which means that there is an absence of the value.
  • You can unwrap the Optionals values.
  • Optionals values are the variables and constant, which contains a value or an  empty box.

Up Next
    Ebook Download
    View all
    View all