Sets - FUNDAMENTALS - Understanding Swift Programming: Swift 2 (2015)

Understanding Swift Programming: Swift 2 (2015)

PART 1: FUNDAMENTALS

6. Sets

A set is an unordered list of elements that contains only one copy of each particular value. A set in Swift has the name Set.

Sets are new in Swift 1.2 and are one of three forms of what are known as collections, or types that contain multiple elements. The others are arrays and dictionaries.

By element I mean an object or some lesser data type such as an integer, string, character, Boolean, or an instance of a custom enumeration or structure.

A set is similar to an array, but is different in two ways according to the definition above. First, it is unordered. You can access a set using an index, but there is no guarantee about what element you might get. Second, a given value can be stored in a set only once. With an array, you can set every element of the array to the same value if you like.

Compared to an array, a set may seem to have a rather limited value. It certainly has a limited range of uses, but is very valuable for those situations in which it is used.

It is sometimes useful to know whether a value has already been dealt with, and thus is can be helpful to keep a list of values, using a data type that does not allow duplicate values.

A set is optimized for what it is good at (such as determining whether a particular values is contained in the set) and is very fast.

Creating a Set

To create a new set, you initialize it using the following syntax:

var fruits = Set<String>()

This creates an empty set and assigns it to the variable fruits, which will be of the type Set<String>, meaning a Set containing elements that are each of the type String.

A set can also be created by initializing it with the same sequence of literal values that is used to initialize an array:

let fruits = Set(["Apple", "Blueberry", "Peach", "Mango"])

You can declare the type explicitly as follows:

let fruits: Set<String> = Set(["Apple", "Blueberry", "Peach", "Mango"])

Determine Whether a Value is Contained in a Set

Once a set has been created, it can be checked to see if a given value is contained in it:

let blueBerryContained = fruits.contains("Blueberry")

The above statement will set the constant blueBerryContained to true if the literal string value "Blueberry" is contained in the set fruits. If that value is not contained in fruits, the constant will be set to false.

Inserting and Removing Values

Once a set has been created, a new value can be inserted into it as follows:

var fruits = Set(["Apple", "Blueberry", "Peach", "Mango"])

fruits.insert("Apricot")

Once a set has been created and contains at least one value, that value can be removed as follows:

var fruits = Set(["Apple", "Blueberry", "Peach", "Mango"])

fruits.remove("Blueberry")

Once a set has been created and it contains values, all of the values can be removed as follows:

var fruits = Set(["Apple", "Blueberry", "Peach", "Mango"])

fruits.removeAll()

Determining the Number of Elements in a Set

Once a set has been created, the number of values contained in it can be determined as follows:

let fruits = Set(["Apple", "Blueberry", "Peach", "Mango"])

let numberOfElements = fruits.count

And once a set has been created, a check can be made of whether it is empty:

let fruits = Set(["Apple", "Blueberry", "Peach", "Mango"])

let isEmpty = fruits.isEmpty

The constant isEmpty will be set to true if fruits is empty; false if it is not empty.

Other Functions

The Set class can also do a number of other functions, including determining if a set is a subset, strict subset, superset, or strict superset of another set, determine whether two sets have duplicate values, combine sets, subtract one set from another, and create a set of common or uncommon members.

Hands-On Exercises

Go to the following web address with a Macintosh or Windows PC to do the Hands-On Exercises.

For Chapter 6 exercises, go to

understandingswiftprogramming.com/6