Next: 5.2 Accessing the remote Up: 5 The client Previous: 5 The client

5.1 The FileReader protocol

Because the FileReader class is not compiled into the client (as we moved it into the server), we can't refer to the FileReader class in the client. And still, if we need to access the remote FileReader object, we need a way to declare which methods it supports.

To manage this situation, we use a protocol. If you know Java, this is similar to an interface in Java. A protocol declares some methods, leaving the implementation unspecified. The language then allows you to declare that a certain object conforms to a certain protocol; this means that the object implements the methods listed in the protocol. In our example, this allows us to declare that we can send the getFile: method to the reader object, without actually knowing the implementation of the method nor actually knowing the class of the reader object.

The declaration of the protocol is as follows:

@protocol FileReader
- (NSString *) getFile: (NSString *)fileName;
@end
This declares the protocol FileReader to have a single method, getFile:. Objects conform to this protocol if and only if they have a getFile: method taking a NSString * argument, and returning an NSString *.

The reader object, which we used to declare to be of class FileReader,

FileReader *reader;
is now declared more generically to conform to the FileReader protocol:
id <FileReader> reader;
id means a generic object; <FileReader> means that it must conform to the FileReader protocol; in this case this simply means that reader is an object and you can send the message getFile: to it.


Next: 5.2 Accessing the remote Up: 5 The client Previous: 5 The client
2011-02-09