Working with Sets
Now that you have an instance of Set
, you might be wondering what to do with it.
The Set
offers a number of methods that allow you to work on sets alone or in combination with another set – or, sometimes, a different collection type.
Some (but not all) of these operations are also available for arrays; most are not available for dictionaries.
The features of the various types are streamlined for their most common use cases.
For example, you might want to know if your groceryList
contains a particular item.
The Set
type provides a method called contains(_:)
that looks inside a set instance for a particular item.
... for food in groceryList { print(food) (3 times) } groceryList.remove("Pears") "Pears" ...
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.