Introduction
Swift is a very safe language, by which I mean it works hard to ensure your code never fails in surprising ways.
One of the most common ways that code fails is when it tries to use data that is bad or missing.
Swift has a solution: optionals. An optional value is one that might have a value or might not.
What are optionals
Optional is just a type in Swift language, nothing fancy. Int and Int? (optional Int) are two different types, if your variable happens to be of type Int you can be absolutely sure it will always have an integer value, and if your variable is of type Int? it will either have an integer value or it will have no value at all (in other words, it will be nil).
Think of optional as a wrapper type. It’s like a gift box which wraps the value inside, and like a real-life box, optional can either contain something or be empty.
An optional which contains integer value of four, as if to write let myOptional: Int? = 4
An optional that doesn’t contain any value, as if to write let myOptional: Int? = nil
The high level idea of wrapping the value inside a box is that we can safely use the box without worrying what’s inside.
Under the hood optional types are merely an enum with two cases — None meaning no value is set and Some meaning the value is set and it’s the value associated with it.
enum optional<T> {
case None
case Some(T)
}
//These two statements are exactly the same
let x: String? = nil
let x = Optional<String>.None
//These two statements are exactly the same
let y: String? = “Hi this is malai”
let y = Optional<String>.Some(“Hi this is malai”)
Forced Unwrapping (!)
// forced unwrapping
let b: String? = "Hi there"
var a = b!
// under the hood forced unwrapping is a switch statement
switch b {
case .Some(let value): a = value
case .None: //raise an exception
}
|
Well, because compiler doesn’t know how to add a box into an integer. In order to achieve this we need to unwrap the optional, in other words we need to open the box and extract the value inside. For that we put the “!” mark after the variable’s name meaning “I’m sure this box contains something, extract that value and use it”. This is called forced unwrapping.
Optional Binding
Like forced unwrapping, optional binding is a way of opening the box, but it does the job more cleverly and with less pollution than forced unwrapping. It allows to check the optional and extract its value into a constant or variable as part of a single action.
let optionalInt: Int? = 5
if let constantInt = optionalInt {
print("optionalInt has an integer value of \(constantInt).")
} else {
print("optionalInt is nil")
}
// will print "optionalInt has an integer value of 5"
|
Implicitly Unwrapped Optionals