Summary

This chapter is packed with complex and interesting information that is essential if you want to write beautiful Swift code. Protocols allow you to write extremely flexible code that doesn't rely on inheritance, which makes it a lot easier to understand and reason about your code. In this chapter, you saw how you can leverage the power of protocols to work with an object's traits or capabilities rather than just using its class as the only way of measuring its capabilities.

Next, you saw how protocols can be extended to implement some default functionality. This enables you to compose powerful types, simply by adding protocol conformance instead of creating a subclass. You also saw how protocol extensions behave depending on your protocol requirements and that it's wise to have anything that's in the protocol extension defined as a requirement. This makes the protocol behavior more predictable.

Finally, you learned how associated types work and how they can lift your protocols to the next level by adding generic types to your protocols that can be tweaked for every type that conforms to your protocol.

The concepts shown in this chapter are pretty advanced, complex, and powerful. To truly master their use, you'll need to exercise yourself to think in terms of traits instead of an inheritance hierarchy. Once you've mastered this, you can experiment with protocol extensions and generic types. It's okay if you don't fully understand these topics right off the bat. They're completely different and new ways of thinking for most programmers with OOP experience.

Now that we've explored some theory behind protocols and value types, let's see how we can put this new knowledge to use by shortly revisiting the HelloContacts app from previous chapters.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.12.162.37