{infiniteZest}
// Articles. Tutorials. Utilities.
Home  |   Search  |   Login  
Categories Skip Navigation Links
New / All
AJAX
Apple
ASP.NET
.NET
Git
Google / Android
Python / IronPython
Miscellaneous
SQL Server
Swift - The Implementation of Optionals
Summary
This article discusses how Optionals are implemented in Swift.
 
Table of Contents

Optional Enum

Optional Enum Declaration

 
Article Series
Previous Article:
Test Your Knowledge: Swift - Introduction to Optionals
This article is part of the Series:
Swift - Optionals
Next Article:
Test Your Knowledge: Swift - The Implementation of Optionals

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.

Take a Quick Quiz on this Article

1. How are Optionals implemented internally in Swift? As …



: Two cases: None, Some<T>
Question 1 of 5
Article Series
Previous Article:
Test Your Knowledge: Swift - Introduction to Optionals
This article is part of the Series:
Swift - Optionals
Next Article:
Test Your Knowledge: Swift - The Implementation of Optionals
Bookmark and Share This

More Articles With Similar Tags
icon-swift-test.jpg
Test on how Optionals are implemented internally in Swift.
icon-swift-article.jpg
This article talks about unwrapping the optionals in Swift implicitly and the circumstances where you would use this functionality.
icon-swift-article.jpg
This article discusses the safer ways of unwrapping an optional in Swift that includes Optional Binding.
icon-swift-article.jpg
This article talks about the Nil Coalescing Operator (??) in Swift.
icon-swift-article.jpg
This article talks about optional chaining in swift where multiple optionals can be chained together and fail gracefully by returning nil when one of them is nil.
About  Contact  Privacy Policy  Site Map