Home > Articles > Programming > General Programming/Other Languages

The Swift Developer's Cookbook: Optionals?!

  • Print
  • + Share This
Learning how to recognize and use optionals is an essential step in mastering the Swift language. This chapter from The Swift Developer's Cookbook introduces optionals and surveys the supporting constructs you need to create, test, and successfully use optionals in your code.
This chapter is from the book

nil happens. When dictionary lookups fail, when instance properties are case dependent, when asynchronous operations have not completed, when failable initializers cannot create instances, and in dozens of other situations where values may or may not have been set, Swift may return nil instead of some other more concrete content. Swift provides nil as a powerful tool for expressing situations in which values are unavailable for use.

Swift differentiates these “no content” scenarios from error handling. In error handling, control flow changes to offer failure mitigation and reporting. With nil, a value-of-no-value represents an absence of data. It offers a testable placeholder to be used when no data is otherwise available.

Unlike in many other languages, in Swift, nil is not a pointer. It is a safe and expressive way to represent the potential for both a valid and invalid value within a single construct. In Swift, the Optional type encapsulates this concept and enables you to differentiate between successful value assignments and nil cases.

Learning how to recognize and use optionals is an essential step in mastering the Swift language. This chapter introduces optionals and surveys the supporting constructs you need to create, test, and successfully use optionals in your code.

Optionals 101

Question marks and exclamation points are the hallmark of Swift optionals. Any variable marked with ? may—or may not—contain a valid value. To get a sense of this language feature, consider the following example:

var soundDictionary = ["cow": "moo", "dog": "bark", "pig": "squeal"]
print(soundDictionary["cow"] == "moo") // prints true
print(soundDictionary["fox"]) // What does the fox say?

What does the fox say? In Swift, the answer is nil. In Swift, nil indicates “no value.” Unlike in Objective-C, in Swift, nil is not a pointer. Swift nil indicates a semantic missing non-existence, a count of Monte Cristo, a cup of “not tea,” an honest politician. A nil item means “nothing to see here, move along, move along, move along.”

In this soundDictionary example, the variable stores a string dictionary. With strings for both keys and values, its type is Swift.Dictionary<Swift.String, Swift.String>. You can also represent this type as [String: String], using square brackets and a colon. Swift infers this type from the concrete data provided on the right-hand side of the soundDictionary assignment. Alternatively, you can use explicit type annotation in your code by adding a colon and a type in the variable declaration:

var soundDictionary: [String: String] =
    ["cow": "moo", "dog": "bark", "pig": "squeal"]

Although the dictionary uses String keys and values, when you look up any item in this dictionary, the value returned is not a String. It’s typed String?. That question mark is critical to understanding dictionaries because it indicates an Optional type. Dictionary lookups may succeed or fail. Optional return types encapsulate both possibilities.

Contrast this behavior with arrays, where it’s the programmer’s job to check whether an index exists before accessing it. Both types could easily be implemented with the other convention, of course, but the Swift people chose the more likely use case for each type. Arrays are highly bounded with a small domain of legal lookup indexes. Dictionaries are often sparse compared against their possible key domain. Optionals enables dictionaries to better represent their “may or may not map to a value” results.

Confirm the return type with Quick Help. Enter the dictionary and the following assignment in Xcode. Then Option-click the sound symbol (as in Figure 3-1) or select the symbol and open View > Utilities > Show Quick Help Inspector. The declaration line in the Quick Help presentation confirms that the type assigned to sound is String?:

var sound = soundDictionary["cow"] // sound is typed String?
Figure 3-1

Figure 3-1 Xcode’s Quick Help reveals symbol typing.

While it might appear that the dictionary returns "moo" on success or nil for failed lookups, this is actually misleading. Print the output to the Xcode console, with print(sound). A successful result looks like Optional("moo"), with its value embedded in an optional wrapper. In this case, the Optional type uses a .Some enumeration, and its associated value is the "moo" string.

You cannot use this optional directly as a string. The following example fails because you must unwrap Optional types before using their values:

let statement = "The cow says " + sound

The + operator in this case works on two string values, not on a string and an optional. Unwrapping gives you access to the string stored within the optional wrapper, enabling you to perform tasks like appending values.

  • + Share This
  • 🔖 Save To Your Account