Chapter 7. Protocols

 

This chapter covers

  • Protocol overview
  • Defining protocols
  • Conforming to a protocol
  • Looking at examples of commonly used protocols

Now that you have some experience creating standard and custom objects, you need to look at how these objects interact. If two objects are to work together, you need to make sure they know how to talk to each other. For objects to talk to each other, they need to know what conversation they’re having. The conversation between objects is usually represented by the protocol design scheme.

Sometimes when you create a class, you design it to be used as a tool for some other class. If you’re going to do this, two things must be in place. First, your object must have a way to send callbacks to whatever class is trying to use it. Second, a class must be able to verify that whatever class is using it has the correct structure for it to communicate whatever it needs. This design pattern is used extensively through the classes provided in the iPhone software development kit (SDK) from Apple. You’ll implement some type of protocol methods in every application you make. Mastering the creation of a protocol for your own class and conforming to protocols for another class are vital to making a sturdy application.

Apple highlights three main motivations for using protocols in your design:

7.1. Defining a protocol

7.2. Implementing a protocol

7.3. Important protocols

7.4. Summary

sitemap