Understanding protocols
Imagine an app used by a fast-food restaurant. The management has decided to show calorie counts for the meals being served. The app currently has the following class, structure, and enumeration, and none of them have calorie counts implemented:
- A
Burger
class - A
Fries
structure - A
Sauce
enumeration
Let's see how you can add calorie counts to all three, and later write a function to calculate the total calorie count.
Add the following code to your playground to declare the Burger
class, the Fries
structure, and the Sauce
enumeration:
class Burger { } struct Fries { } enum Sauce { case chili case tomato }
These represent the existing class, structure, and enumeration in the app. Don't worry about the empty definitions, as they are not required for this lesson. As you can see, none of them have calorie counts.
You will resolve this issue by using protocols...