Chapter 6. Make Swift Work for You – Protocols and Generics
As you learned in Chapter 2, Building Blocks – Variables, Collections, and Flow Control, Swift is a strongly typed language, which means that every piece of data must have a type. Not only can we take advantage of this to reduce clutter in our code, we can also leverage it to let the compiler catch bugs for us. The earlier we catch a bug, the better. Besides not writing them in the first place, the earliest place we can catch a bug is when the compiler reports an error.
The two big tools that Swift provides to achieve this are called protocols and generics. Both of them use the type system to make our intentions clearer to the compiler, so that it can catch more problems for us.
In this chapter, we will cover:
- Protocols
- Generics
- How to extend the existing generics
- How to put protocols and generics to use