By Rajiv Ramnath

By definition a protocol clarifies sets of methods that can be implemented by any class for an iOS app. Protocols are used to define interfaces that are implemented by classes. Protocols are often used to specify the interface for delegate objects. With the protocol feature, you can avoid having to force subclassing in order to create polymorphic classes.

Objective-C (unlike C++) doesn’t allow a class to have multiple superclasses. Protocols therefore are an essential feature in Objective-C because it doesn’t allow multiple inheritance.

When a class implements the methods in a protocol, the class is said to conform to the protocol.

The definition of a protocol is similar to the definition of an interface for a class. However, a protocol doesn’t have a parent class or instance variables. Here’s an example of a protocol definition (Printable) from the postfix calculator program:

@protocol Printable
  -(void) printMe; // I can leave out @required, since it is the default
  @optional -(NSString*) printMeToString;

With the arrival (and indeed with great fanfare) of Objective-C 2.0, protocol methods can be either required (the default) or optional. Notice that the compiler directives are here as well.

Here is the declaration of the StackOfInteger stating that it will implement the protocol:

@interface StackOfInteger:NSObject <Printable>

Here is the implementation of the protocol in the StackOfInteger class:

@implementation StackOfInteger;
 … Other StackOfInteger methods …
/////////// Methods to implement the Printable protocol ////////////
- (void) printMe{
 int k, stackCount = [elements count];
 NSLog (@"Stack->Last >%d< n", self->last);
 for(k=0; k<stackCount; k++) {
  NSLog (@"Element at [%d] = >%@<n", k, [elements objectAtIndex:k]);

Notice that the printMeToString method isn’t implemented because it’s optional.

A program can test whether an object conforms to a protocol. The following code includes examples of this test (taken from the main program of the sample):

 printf("Testing conformance to protocolsn");
 if ([myStack conformsToProtocol:@protocol(Printable)]) {
  printf("Using protocol methodsn");
  [myStack printMe];
 } else {
  NSLog(@"Object does not conform to protocol %@n", @protocol(Printable));
 id someObject = [[PostfixCalculator alloc] initWithExpression:@"44 55 *"];
 if (![someObject conformsToProtocol:@protocol(Printable)]) {
  NSLog(@"Object does not conform to protocol");

Because the myStack object conforms to the protocol, its printMe and printMeToString methods are called. Since someObject (which is type id to make the example more meaningful) doesn’t conform to the Printable protocol, the test for conformity fails.

You can also test for the existence of an optional method using the method respondsToSelector.

You can make a class adopt a protocol simply by implementing the methods defined by that protocol. That is, you don’t have to declare the protocol in its interface. However, even though the class implements the protocols methods, it will not be conforming to the protocol and will fail the preceding conformance test.