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; @endThis 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.