December 2015
Intermediate to advanced
400 pages
13h 3m
English
It is time to make an instance of Set. Create a new playground called Groceries.
Type in the following to get an instance of Set.
Listing 11.1 Creating a set
var groceryBag = Set<String>()
You made an instance of Set and declared that it will hold instances of the String type. It is a mutable Set called groceryBag and is currently empty. Let’s fix that.
You can add groceries to your groceryBag by using the insert(_:) method.
Listing 11.2 Adding to a set
var groceryBag = Set<String>()
groceryBag.insert("Apples")
groceryBag.insert("Oranges")
groceryBag.insert("Pineapple")
Now groceryBag has a few items inside of it. As with arrays and dictionaries, you can loop through a set to see its contents. ...
Read now
Unlock full access