Challenge D: Nested optionals

Consider the following nested optional. It corresponds to a number inside a box inside a box inside a box.

let number: Int??? = 10

If you print number you get the following:

print(number)

// Optional(Optional(Optional(10)))

print(number!)

// Optional(Optional(10))

  1. Fully force unwrap and print number.
  2. Optionally bind and print number with if let.
  3. Write a function printNumber(_ number: Int???) that uses guard to print the number only if it is bound.

So far, you’ve mostly seen data in the form of single elements. Although tuples can have multiple pieces of data, you have to specify the size up front; a tuple with three strings is a completely different type from a tuple with two strings, and converting between them isn’t trivial.In this section, you’ll learn about collection types in Swift. Collections are flexible “containers” that let you store any number of values together.

There are several collection types in Swift, but the two most important are arrays and dictionaries. You’ll learn about them over the first two chapters in this section:

Chapter 8, Arrays

    • Chapter 9, Dictionaries

Next you will learn how you can apply custom operations and loop over collection types with:

Chapter 10, Collection Iterations With Closures

The collection types have similar interfaces but very different use cases. As you read through these chapters, keep the differences in mind, and you’ll begin to develop a feel for which type you should use when.

As part of exploring the differences between the collection types, you’ll also consider performance: how quickly the collections can perform certain operations, such as adding to the collection or searching through it.

The usual way to talk about performance is with big-O notation. If you’re not familiar with it already, read on for a brief introduction.

results matching ""

    No results matching ""