Set

The Set type is a generic collection that is similar to the array type. While the array type is an ordered collection that may contain duplicate items, the Set type is an unordered collection where each item must be unique.

Similar to the key in a dictionary, the type stored in an array must conform to the Hashable protocol. This means that the type must provide a way to compute a hash value for itself. All of Swift's basic types, such as String, Double, Int and Bool, conform to the Hashable protocol and can be used in a set by default.

Let's look at how we would use the Set type.

Initializing a set

There are a couple of ways in which we can initialize a set. Just like the array and dictionary types, Swift needs to know what type of data is going ...

Get Mastering Swift 2 now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.