Chapter 3. Collecting and Controlling: Going Loopy for Data

image

You already know about expressions, operators, variables, constants, and types in Swift. It’s time to consolidate and build on that knowledge and explore some more advanced Swift data structures and operators: collections and control flow. In this chapter, we’re going to talk about putting collections of data into variables and constants, and how to structure data, manipulate data, and operate on data using control flow statements. We’ll be looking at other ways to collect and structure data later in the book, but for now let’s get started with arrays, sets, and dictionaries.

Sorting pizzas

image

Meet the Swift Pizzeria, the fastest pizza place in town.

Note

Get it? Hilarious.

image

The Chef, who is pretty grumpy at the best of times, wants to sort the restaurant’s pizzas alphabetically using Swift (after all, it’s in the name).

image

They’re facing some issues, though. Can you help the chef sort their pizzas?

Swift has a range of special types that can store collections of things. Unsurprisingly, these are called collection ...

Get Head First Swift now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.