No doubt, Apple’s Swift programming language is most popular among developers for iOS, macOS, tvOS & watchOS developments. This language has gained high popularity due to its easy to understand syntax, protocol oriented approach of coding i.e unique when compared to any other programming languages and tons of predefined libraries for Apple’s developer’s community.
Today, I will be talking about a very important concept in the Swift programming language where most of the beginner developers face a lot of problems and gets confused i.e Optional, the funky ‘?’ mark sign used while coding. By the end of this article, you will have a clear concept about, what is optional and why and where you should use them?
When I studied, whatever the source may be, I found that most of the instructor or developers use ‘?’, i.e the optional, unwrap them, but why do the programmers use this which is very unique in Swift? In Java or C++, the concept near to optional is null or nil type.
Why do Optional Exist?
Before I talk about what is optional and why Apple engineers came up with concepts like optional, let us first discuss some basic rules while programming in Swift which is:
- Every variable in Swift must be defined as explicit/Implicit.
- The type can be inferred based on the value.
- Every normal type must have a value associated with them.
For Example:
let name: String = “ABC” // Explicit Example
let myname = “ABCD” // Implicit Example
So, if you declare any variable there must be some value or type associated with it. If you write something like this let’s say, let myage and try to print by writing, print(myage), then this type code will not work in Swift because we need some value associated with the declared variable.
Look at the diagram below and try to think a bit…
What comes into your mind when you see this image? The general concept behind programming is that we make functions to perform the certain tasks. Here, Function 1 is supposed to return an integer value that goes to Function 2 and that is supposed to return a string. This string goes to Function 3 that actually returns a double value.
So, basically, a chain exists and each function actually depends on its predecessor to return some data in different forms of data types. Suppose, if any of the functions fail to return their value then, what will happen?
Yes, the system or the app will crash, i.e. it will not work. you must be thinking why the function will not return anything? Well, there can be certain cases while programming where, you had declared a variable inside a particular function which was supposed to have a value but actually found nil while running the program, this could generally happen when we are fetching some data from an API or local server, recording or collecting data from the outer world.
If our program finds the value of declared variable nil, no value will be passed from such functions to its successor, leading to crash of our program i.e apps in most cases, because in Swift, as we saw above, the code will not work if we don’t have any value associated with a declared variable.
So, to get rid of these problems, the concept of optional was introduced.
What are Optionals?
Apple’s engineer came up to solve the above-discussed problem in a very smooth and intelligent manner with a concept what we call as optional. While declaring any variable for example say;
var number: Int?
this ‘?’ is actually is a kind of notation and the variable associated with optional is called an optional type variable. What does this mean then?
A variable declared with an optional is basically a safe candidate i.e if we feel like there could circumstances, where the value of our declared variable could be found nil then, using the safe unwarping concept for optional, the crashing of the app could be avoided. This is basically a protection mechanism to avoid crashing of applications due to exceptionally found nil variables.
So, now, you have got some of your answers i.e what is optional, where and when it should be used.