O'Reilly logo

Mastering Swift 2 by Jon Hoffman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required