Chapter 3. Understanding Objective-C Protocol and Table View

In the previous chapter, we had a fair enough introduction of the Core Data framework and its components. We also learnt about the Data Model and its different terms including Entities, Properties, Attributes, Relationships, Inverse Relationships, and so on. We also explored the concepts of the Managed Object Model, Managed Object, and Managed Object Context. Finally, we saw the relationships among different components of Core Data API: Persistent Store, Persistent Store Coordinator, Fetch Request, and FetchedResultsController. Besides this, we also had a quick overview of the entire application that we will be building in this book from chapters 4 through 11.

As said in Chapter 1, Overview, for a better understanding of the Core Data, we need to have a good knowledge of two important concepts: How classes can adopt various protocols in Objective-C and knowledge of how information is displayed via Table View control. So, before we begin developing our application—Sales Record System for Departmental Store—we need to learn the first concept, which is about Protocols—what they are, how the methods are declared in a protocol, and the role of the delegate that conforms to the protocol. In other words, we can say that we're going to learn how a class receives tasks performed by another class via the Delegation Pattern.

You might be thinking, why it is necessary to understand the concept of protocol for developing our Core Data application. The answer is simple. While developing our application (which will begin from Chapter 5, Creating, Listing, and Deleting Names of Customers), we will realize that the main class of the application is the implementation file: RootViewController.m and its objects will adopt protocols, so that they can be used by other objects in a well-defined manner. That is, the main implementation file, RootViewController.m, will implement the methods of the protocols defined in other classes of the application.

This chapter will help you to better understand what we mean by the terms protocol, delegate, methods, and so on, through a running application.

Before we begin with the protocols, let's get a quick idea of the two patterns, Delegation pattern and Strategy pattern:

  • The Delegation pattern is usually used to allow reuse of objects by allowing a helper object, or delegate, to assume the responsibility of customizing the behavior of the existing object without modifying its code. That is, a delegate is usually used to take some action at a defined hook. This is an alternative to customizing the behavior of a class via subclassing.
  • In Strategy pattern, we have a few operations that will perform a calculation as per the strategy of the class that adopts the protocol. Regardless of how this is implemented, the implementation will only ever return a calculated value. The calculated values will not alter or customize the behavior of the object that is making use of the delegate.

Let's see how we can declare and enforce the behavior of delegate objects through protocols.