Book Image

Swift Cookbook

By : Cecil Costa, Cecil Costa
Book Image

Swift Cookbook

By: Cecil Costa, Cecil Costa

Overview of this book

Table of Contents (18 chapters)
Swift Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Working with sets


Swift brought to us this new feature called tuples, but for some unexplained reason, the set type has disappeared, or at least, it's not implemented yet. We have two solutions: the first one is using the old NSSet, which will be demonstrated in this recipe, and another solution is to create a new set class, which will be shown in the next recipe.

For this recipe, the user will write on a text field the cities that he has already visited, and when he finishes, he will press a button and show the cities without repetition.

Getting ready

Let's create a Swift project called Chapter2 NSSet.

How to do it...

To demonstrate working with NSSet, follow these steps:

  1. Click on the view controller and add a text field where the user will write a city name, a button that the user will press when he finishes writing the city name, and another button to display the introduced cities.

  2. Now, go to the view controller and add the following attributes:

    var cities:NSMutableSet = NSMutableSet()
    @IBOutlet...