Book Image

Swift Cookbook

Book Image

Swift Cookbook

Overview of this book

Table of Contents (18 chapters)
Swift Cookbook
About the Author
About the Reviewers

Creating your own set

As we can see with the previous recipe, sometimes, our old friend NSSet is not enough to solve our problems, so in this recipe, we will create our own set. The goal of this recipe is not only to show how you could create your own set, but also to overload operators.

For this recipe, we will create a simple shopping list program, where the user can write the product that he needs to buy and its quantity. If he tries to add it twice, the product will not appear twice, rather it will sum the quantity to the existing product.

There will be a switch button that when disabled means the user won't be able to add the product to the shopping list if it already exists, and of course, there will be a button to display our list.

Getting ready

As usual create a new project called Chapter2 ShoppingList, and then let's create a Swift file called ShoppingList. The idea here is to create our container and the type, which it will store, in this case, the Product class.

How to do it...