Set

We need to know about sets in order to make a touch-sensitive app, because the operating system delivers a set of touch objects to the view that was touched. There may be more than one touch in progress at a given moment.

A collection is a big object that can contain little values. The simplest example of a collection is a set. A set contains only the little values, but no additional information about them such as which value comes first and which one last. If we need to remember their order, then what we want is an array, not a set.

The application(_:didFinishLaunchingWithOptions:) method of the application delegate creates a Set<String> object containing five String objects. These values contained in the set are called the elements of the set. The count property of a Set is a non-negative integer giving the number of elements.

The {curly braces} around the bodies of the for loop and the if statements are required. borough does not need to be declared; it is implicitly declared to be a different constant at the start of each loop. Since our collection is a set, rather than an array, there is no guarantee that the for loop will visit the elements in the order in which we inserted them.

The first property of a set returns an optional value. We should unpack the optional value with ! only if we have first made sure that the set is not empty.

Apple’s Documentation

  1. Sets in the Swift Language Guide. A set is an example of a collection.
  2. struct Set in the Swift Standard Library Reference. It conforms to protocol CollectionType.
  3. Classes NSSet and NSMutableSet are left over from the language Objective-C. You can still use them in Swift, but don’t. See Sets: Unordered Collections of Objects.

Source code in Set.zip

  1. AppDelegate.swift: the application(_:didFinishLaunchingWithOptions:) method of class AppDelegate contains a demonstration of struct Set<String>.
  2. Main.storyboard
  3. Info.plist: unchanged.

Output from print

The for loop did output all five strings, but not in the original order.

boroughs.count = 5
Bronx
Manhattan
Queens
Staten Island
Brooklyn
Yonkers is not a borough.
The borough of the week is Bronx.

Things to try

  1. We can print the entire set:
    		//Two ways to print the same String.  Also try debugDescription.
    		print(boroughs.description);
    		print(boroughs);
    
    ["Bronx", "Manhattan", "Staten Island", "Brooklyn", "Queens"]
    

  2. Create the set with var instead of let. Then change the contents of the set with the methods insert and delete.
    		var boroughs: Set<String> = Set<String>(); //Create an empty set.
    		boroughs.insert("Bronx");
    		boroughs.insert("Brooklyn");
    		boroughs.insert("Manhattan");
    		boroughs.insert("Queens");
    		boroughs.insert("Staten Island");
                    //not guaranteed to stay in the above order
    
    		boroughs.insert("Yonkers");
    		let removed: String? = boroughs.remove("Yonkers");
    		if removed == nil {
    			print("remove failed");
    		} else {
    			print("removed the string \(removed!)");
    		}
    

    You can also create an empty set this way:

    		var boroughs: Set<String> = Set(); //Create an empty set.
    

  3. Change the five elements to 10, 20, 30, 40, 50. You will have to change boroughs from a Set<String> to a Set<Int>, and change b from a String to an Int. Change "Yonkers" to 20.

  4. The following pair of nested loops will print every combination of “one from group A and one from group B”, also known as the Cartesian product of two sets.
    		let parties: Set<String> = [
    			"Democratic",
    			"Republican"
    		];
    
    		let leanings: Set<String> = [
    			"liberal",
    			"moderate",
    			"conservative"
    		];
    
    		for party in parties {
    			for leaning in leanings {
    				print("\(party) \(leaning)");
    			}
    		}
    
    Democratic moderate
    Democratic conservative
    Democratic liberal
    Republican moderate
    Republican conservative
    Republican liberal
    

    Can you list the combinations leaning by leaning, rather than party by party?