Optional Enum
We know that an optional can be declared by using a question mark at the end of the type. For example, in the code below, the variable score is of type Optional<Int>. It can take nil as value (absence of value) and an Int.:
var score: Int?
score = 20
print(score)
// Prints: Optional(20)
score = nil
print(score)
// Prints: nil
So, here the type Int is wrapped inside Optional. In Swift, the Optional<Wrapped> is actually an enum. And the angled brackets <> represent that it is implemented via generics. The parameter inside represents any type (you would typically use letters like T, U, V, etc. or you can use a more meaningful word like Wrapped here to represent a Type).
Optional Enum Declaration
Let’s look at how Optional enum is declared in Swift. The protocols and some methods are not shown for clarity.
// Protocols at the end of this line are omitted
public enum Optional<Wrapped> {
// Two cases - None and Some<Wrapped>
case None
case Some<Wrapped>
// Initializers for nil and non-nil (only two are shown)
public init()
public init(_ some: Wrapped)
// Other methods, initializers omitted
}
As you can see, the Optional enum has two cases: None and Some<Wrapped>. The case None represents nil — a no-value state and Some<Wrapped> represents some value of the type Wrapped.
So, the Optional written with question mark (?) can also be written like this with Optional enum:
var score: Optional<Int>
score = Optional.Some(20)
print(score)
// Prints: Optional(20)
score = Optional.None
print(score)
// Prints: nil
In the above code, score is of type Optional<Int> enum. The Optional enum has two cases, and when Some(20) is used, the value of score becomes Optional(20). And when None is used, the value becomes nil (a no-value state).
For example, the initializers in the Optional enum can be used in the following way:
var score = Optional<Int>(20)
print(score)
// Prints: Optional(20)
var score2 = Optional<Int>()
print(score2)
// Prints: nil
In the above, two initializers in the Optional<Wrapped> enum are shown in action. First initializer takes a non-nil value of type specified in the angled brackets and the one initializes to the no-value state.
Obviously, the above code with Optional<Int> is shown to provide a deeper understanding of the implementation of Optionals. When you declare an optional though, you want to use the short/precise/more readable ? (question mark) format.