Moving Between Types
As you have seen, each collection type has a feature set appropriate to the type’s primary uses: arrays as ordered lists, dictionaries as key-value pairs, and sets modeling mathematical sets as bags of unique values. Some of these types can be initialized with data from the others. It will be especially common for you to want to compare the data from two arrays in a way that only sets support. The good news is that you can create sets from your arrays – but you must bear in mind the differences between those types.
For example, imagine playing some games with friends after you arrive home from your grocery trip. Create an array of players and an array to track who won each game:
... let players ...
Get Swift Programming: The Big Nerd Ranch Guide, 3rd Edition 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.