Using protocols to define interfaces
Protocols are a way to describe the interface that a type provides. They can be thought of as a contract, defining how you can interact with instances of that type.
Protocols are a great way to abstract what something does from how it does it. As we will see in subsequent chapters, Swift adds functionalities to protocols that make them even more useful and powerful than in many other programming languages.
Getting ready
We will continue to build on examples from the previous recipes, but don’t worry if you haven’t followed these recipes yet as all the code you need is listed in the upcoming sections.
How to do it…
In the previous recipe, we added a method to our Person
class that (given the full implementation) would save it to a remote database. This is a very useful functionality, and as we add more features to our app, there will likely be more types that we also want to save to a remote database:
- Create...