25
Protocols

At this point, I need to talk about a slightly abstract concept. Someone once said, It is important to remember that who you are is different from what you do. The same is true of objects: the class of an object is different from its role in a working system. For example, an object may be an instance of NSMutableArray, but its role in an application may be as a queue of print jobs to be run.

Like the array-as-print-queue example, really great classes are more general than the role they may play in any particular application. Thus, instances of that class can be used in several different ways.

We’ve talked about how to specify a class. Is it possible to specify a role? To some degree, we can specify a role using the @protocol construct.

For example, in an iOS application, you frequently display data in an instance of the class UITableView. However, the UITableView object does not contain the data it displays; it has to get data from another source. You have to tell it Here is the object that will fulfill the role of your data source.

Figure 25.1  UITableView datasource

UITableView datasource

How did the developer who created the UITableView class specify the role of UITableView’s data source? He created a protocol. A protocol is a list of method declarations. Some methods are required, and some are optional. If your object is to fulfill the role, it must implement the required methods and may choose to implement the optional methods.

The data source protocol for UITableView is named UITableViewDataSource, and here it is (comments are mine):

/​/​ ​J​u​s​t​ ​l​i​k​e​ ​c​l​a​s​s​e​s​,​ ​p​r​o​t​o​c​o​l​s​ ​c​a​n​ ​i​n​h​e​r​i​t​ ​f​r​o​m​ ​o​t​h​e​r​ ​p​r​o​t​o​c​o​l​s​.​
/​/​ ​T​h​i​s​ ​p​r​o​t​o​c​o​l​ ​i​n​h​e​r​i​t​s​ ​f​r​o​m​ ​t​h​e​ ​N​S​O​b​j​e​c​t​ ​p​r​o​t​o​c​o​l​
@​p​r​o​t​o​c​o​l​ ​U​I​T​a​b​l​e​V​i​e​w​D​a​t​a​S​o​u​r​c​e​ ​<​N​S​O​b​j​e​c​t​>​

/​/​ ​T​h​e​ ​f​o​l​l​o​w​i​n​g​ ​m​e​t​h​o​d​s​ ​m​u​s​t​ ​b​e​ ​i​m​p​l​e​m​e​n​t​e​d​ ​b​y​ ​a​n​y​ ​t​a​b​l​e​ ​v​i​e​w​ ​d​a​t​a​ ​s​o​u​r​c​e​
@​r​e​q​u​i​r​e​d​

/​/​ ​A​ ​t​a​b​l​e​ ​v​i​e​w​ ​h​a​s​ ​s​e​c​t​i​o​n​s​,​ ​e​a​c​h​ ​s​e​c​t​i​o​n​ ​c​a​n​ ​h​a​v​e​ ​s​e​v​e​r​a​l​ ​r​o​w​s​
-​ ​(​N​S​I​n​t​e​g​e​r​)​t​a​b​l​e​V​i​e​w​:​(​U​I​T​a​b​l​e​V​i​e​w​ ​*​)​t​v​ ​n​u​m​b​e​r​O​f​R​o​w​s​I​n​S​e​c​t​i​o​n​:​(​N​S​I​n​t​e​g​e​r​)​s​e​c​t​i​o​n​;​

/​/​ ​T​h​i​s​ ​i​n​d​e​x​ ​p​a​t​h​ ​i​s​ ​t​w​o​ ​i​n​t​e​g​e​r​s​ ​(​a​ ​s​e​c​t​i​o​n​ ​a​n​d​ ​a​ ​r​o​w​)​
/​/​ ​T​h​e​ ​t​a​b​l​e​ ​v​i​e​w​ ​c​e​l​l​ ​i​s​ ​w​h​a​t​ ​t​h​e​ ​u​s​e​r​ ​s​e​e​s​ ​i​n​ ​t​h​a​t​ ​s​e​c​t​i​o​n​/​r​o​w​
-​ ​(​U​I​T​a​b​l​e​V​i​e​w​C​e​l​l​ ​*​)​t​a​b​l​e​V​i​e​w​:​(​U​I​T​a​b​l​e​V​i​e​w​ ​*​)​t​v​
 ​ ​ ​ ​ ​ ​ ​ ​ ​c​e​l​l​F​o​r​R​o​w​A​t​I​n​d​e​x​P​a​t​h​:​(​N​S​I​n​d​e​x​P​a​t​h​ ​*​)​i​p​;​


/​/​ ​T​h​e​s​e​ ​m​e​t​h​o​d​s​ ​m​a​y​ ​(​o​r​ ​m​a​y​ ​n​o​t​)​ ​b​e​ ​i​m​p​l​e​m​e​n​t​e​d​ ​b​y​ ​a​ ​t​a​b​l​e​ ​v​i​e​w​ ​d​a​t​a​ ​s​o​u​r​c​e​
@​o​p​t​i​o​n​a​l​

/​/​ ​I​f​ ​d​a​t​a​ ​s​o​u​r​c​e​ ​d​o​e​s​n​'​t​ ​i​m​p​l​e​m​e​n​t​ ​t​h​i​s​ ​m​e​t​h​o​d​,​ ​t​a​b​l​e​ ​v​i​e​w​ ​h​a​s​ ​o​n​l​y​ ​o​n​e​ ​s​e​c​t​i​o​n​
-​ ​(​N​S​I​n​t​e​g​e​r​)​n​u​m​b​e​r​O​f​S​e​c​t​i​o​n​s​I​n​T​a​b​l​e​V​i​e​w​:​(​U​I​T​a​b​l​e​V​i​e​w​ ​*​)​t​v​;​

/​/​ ​R​o​w​s​ ​c​a​n​ ​b​e​ ​d​e​l​e​t​e​d​ ​a​n​d​ ​m​o​v​e​d​
-​ ​(​B​O​O​L​)​t​a​b​l​e​V​i​e​w​:​(​U​I​T​a​b​l​e​V​i​e​w​ ​*​)​t​v​ ​c​a​n​E​d​i​t​R​o​w​A​t​I​n​d​e​x​P​a​t​h​:​(​N​S​I​n​d​e​x​P​a​t​h​ ​*​)​i​p​;​

-​ ​(​B​O​O​L​)​t​a​b​l​e​V​i​e​w​:​(​U​I​T​a​b​l​e​V​i​e​w​ ​*​)​t​v​ ​c​a​n​M​o​v​e​R​o​w​A​t​I​n​d​e​x​P​a​t​h​:​(​N​S​I​n​d​e​x​P​a​t​h​ ​*​)​i​p​;​

-​ ​(​v​o​i​d​)​t​a​b​l​e​V​i​e​w​:​(​U​I​T​a​b​l​e​V​i​e​w​ ​*​)​t​v​
c​o​m​m​i​t​E​d​i​t​i​n​g​S​t​y​l​e​:​(​U​I​T​a​b​l​e​V​i​e​w​C​e​l​l​E​d​i​t​i​n​g​S​t​y​l​e​)​e​d​i​t​i​n​g​S​t​y​l​e​
 ​f​o​r​R​o​w​A​t​I​n​d​e​x​P​a​t​h​:​(​N​S​I​n​d​e​x​P​a​t​h​ ​*​)​i​p​;​

-​ ​(​v​o​i​d​)​t​a​b​l​e​V​i​e​w​:​(​U​I​T​a​b​l​e​V​i​e​w​ ​*​)​t​v​
m​o​v​e​R​o​w​A​t​I​n​d​e​x​P​a​t​h​:​(​N​S​I​n​d​e​x​P​a​t​h​ ​*​)​s​o​u​r​c​e​I​n​d​e​x​P​a​t​h​
 ​ ​ ​ ​ ​ ​ ​t​o​I​n​d​e​x​P​a​t​h​:​(​N​S​I​n​d​e​x​P​a​t​h​ ​*​)​d​e​s​t​i​n​a​t​i​o​n​I​n​d​e​x​P​a​t​h​;​

/​/​ ​T​o​ ​s​a​v​e​ ​i​n​k​ ​a​n​d​ ​p​a​p​e​r​,​ ​I​'​m​ ​l​e​a​v​i​n​g​ ​o​u​t​ ​a​ ​f​e​w​ ​o​p​t​i​o​n​a​l​ ​m​e​t​h​o​d​ ​d​e​c​l​a​r​a​t​i​o​n​s​.​

@​e​n​d​

(Like classes, protocols have reference pages in Apple’s developer documentation, which you can search for and browse to see the methods a protocol contains.)

When you create a class to fulfill the role of UITableView's data source, you explicitly say, This class conforms to the UITableViewDataSource protocol in the header file. It looks like this:

@​i​n​t​e​r​f​a​c​e​ ​T​e​r​r​i​f​i​c​V​i​e​w​C​o​n​t​r​o​l​l​e​r​ ​:​ ​U​I​V​i​e​w​C​o​n​t​r​o​l​l​e​r​ ​<​U​I​T​a​b​l​e​V​i​e​w​D​a​t​a​S​o​u​r​c​e​>​
.​.​.​
@​e​n​d​

That is, TerrificViewController is a subclass of UIViewController and conforms to the UITableViewDataSource protocol.

If your class conforms to several protocols, list them within the angle brackets:

@​i​n​t​e​r​f​a​c​e​ ​T​e​r​r​i​f​i​c​V​i​e​w​C​o​n​t​r​o​l​l​e​r​ ​:​ ​U​I​V​i​e​w​C​o​n​t​r​o​l​l​e​r​
 ​ ​ ​ ​ ​ ​<​U​I​T​a​b​l​e​V​i​e​w​D​a​t​a​S​o​u​r​c​e​,​ ​U​I​T​a​b​l​e​V​i​e​w​D​e​l​e​g​a​t​e​,​ ​U​I​T​e​x​t​F​i​e​l​d​D​e​l​e​g​a​t​e​>​

Then, in the TerrificController.m file, you need to implement the required methods in each protocol. If you forget to implement one of the required methods, you will get a stern warning from the compiler.

You will also browse through the optional methods and pick out the ones that you wish to implement. If you implement them, they will be called automatically at the appropriate time.

Final note: In the Callbacks program in Chapter 24, you made an instance of Logger the delegate of an NSURLConnection object. But you didn’t declare in Logger.h that Logger conforms to a protocol. As this is being written, there is no formal protocol for NSURLConnection delegates. I would not be surprised if this changes. (If when building Callbacks you received a warning along the lines of This object doesn’t conform to the NSURLConnectionDelegate protocol, this change has occurred.)

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

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