Optionals in Swift
By: Ahmed Menaim
Tags: iOS, Xcode, Optional, Programming, Swift

If you are interested in iOS Development or like to start using Swift then you need to know that you will meet some symbols that you may not meet before, marks like question mark ? or exclamation mark !.

Optional is a great feature produced by swift to avoid the nil values for the variables, and you can discover it by seeing a question mark ? which means that this variable is optional or by seeing ! mark which means that you don’t need this variable to be optional anymore.

Suppose you have some movies but each one has its own name and you will watch it at a specific time.

class Movie {

var movieName: String?

var movieQuality: Int?

var movieTime: String?

}

Then you tried to print your movie name to check it:

var optionalMovie = Movie()

print(optionalMovie.movieName)

Output: nil

So here we defined a variable called movieName from String and identified it as an optional by using the ? mark, and the default value for movieName is nil which means that this variable is empty by default. That’s why we were facing a warning while trying to print the name before giving it a value and it will print nil.

What should we do to avoid these warnings? There are many ways to avoid this, let’s figure them out:

if optionalMovie.movieName == nil {

print("Sorry, the movie has no name")

}

else {

print("optionalMovie name is \(optionalMovie.movieName)")

}

Output: Sorry, the movie has no name

The previous code shows how we can avoid the nil value but still we have another warning in the print statement which is:

This warning means that we need to provide a default value for the name to avoid any errors in the future, especially if we have any variable dependent on this name.

if optionalMovie.movieName == nil {

print("Sorry, the movie has no name")

}

else {

print("optionalMovie name is \(optionalMovie.movieName!)") //Sorry, the movie has no name

}

Output: Sorry, the movie has no name

The only difference here is that we added ! at the end of optionalMovie.movieName which silenced the warning, and this ! mark means that I’m sure that there is a value for the variable name, because if the variable is not equal to nil you’re 100% sure that the variable has a value.

guard optionalMovie.movieName != nil else {

print("This movie has no name")

return

}

print("movie name is \(optionalMovie.movieName!)")

The guard statement here is used to check our variable, and if the variable has a value, it will skip the else statement and go through the last print statement line.

We didn’t assign a value to it so the output will be:

Output: This movie has no name

It doesn’t make sense that you will watch a movie if you don’t know its name, and that’s why we will use this solution which is setting a value for the variable:

optionalMovie.movieName = "The fault in our stars"

if optionalMovie.movieName == nil {

print("Sorry, the movie has no name")

}

else {

print("optional movie's name is \(optionalMovie.movieName!)")

}

Output: optional movie’s name is The fault in our stars

Here I put the ! mark as I’m sure that the movieName already has a value since I already initialized it before.

The last solution is to use the if let statement “Optional Binding”:

if let movieName = optionalMovie.movieName {

print("optional movie's name is \(movieName)")

}

else {

print("Sorry, the movie has no name")

}

Output: optional movie’s name is The fault in our stars

We have put our variable in a constant and used this constant in our conditions.

We don’t need to put any marks ? or ! as we already put it into another constant.