Interview Questions: What is Optional in Swift?

Update: I've published a book that helps you prep and ace iOS interviews:

There are a lot of questions you could be asked on tech iOS interviews and one of the most common these days is about Optionals and nil in Swift and Objective-C.

This is another fundamental Swift question that you should be expecting on iOS interviews. Different from Objective-C treatment of nils and introduction of Optionals makes Swift development style in some cases dramatically different from Objective-C. Be ready to potentially talk in length about the big picture architectural implications of that in Swift and how it is going to affect how you're writing your code.

Expected answer:

In Objective-C nil used to be a very handy "value" for variables. It typically meant an absence of value, simply "nothing". You could send a message to a nil and instead of your app blowing up with an exception it would simply ignore it and do nothing (or return nil). In Swift, though, with introduction of let and var it became apparent that not all of the constants and variables can be defined and set at the time of declaration. We needed to somehow declare that a variable is undetermined yet and potentially could have a value or could have no value at all. That's where Optional comes into play.

Optional is defined with ? appended at the end of the variable type you declare. You can set a value to that variable right away or at a later time or not even set it at all. When you use Optional variables you have to either explicitly unwrap them, using ! at the end of the variable, to get the value stored in it or you could do a so-called Optional Binding to find out whether an optional contains a value. To do that you'd use a

if let unwrappedOptional = someOptional { 
  // your code here 


Optionals cannot be used with constants (lets) because a constant has to be defined at the time of its declaration and therefore has value is not an Optional.

Unlike in Objective-C in Swift sending messages to nil causes a runtime exception. But there is a way to do something somewhat similar in Swift where you could send a message to an Optional but if optional is a nil than instead of raising an exception it will just ignore it and return nil, much like the old Objective-C behavior. That allows you to do method chaining calls and if one of the optional values in the call sequence is nil then the whole chain will return nil.

Optionals make Swift lean more towards the functional side of programming languages partially mimicking Maybe concept of Haskel and similar languages. At the end of the day just like let, var, and nil Optional is a helpful construct that forces you to be more mindful of you handle the state of your applications.

In general, you should be very mindful of the state of your applications and use nil and consequently Optionals to represent an absence of value as less as possible. Every time you declare an Optional ask yourself a few times if you really really need it.

NOTE: Objective-C now has nonull and nullable directives to give it more Swift-like
explicit variable type declaration.

Red flag:

Besides not knowing what optionals are and how to work with them the biggest red flag would be to speak in favor of Optional Binding and explicit Optional Unwrapping. The former leads to poor design and cognitive overhead of if/else statements and the latter to the potential danger of runtime exceptions.

This question and many other commonly asked iOS interview questions are covered in details in my upcoming book The iOS Interviews Guide. Click the link if you'd like to sign up to the waiting list. You'll get helpful tips and questions and answers to prepare for your next iOS interview.

Update: I've published a book that helps you prep and ace iOS interviews: