Objective-C: Protocols

In Objective-C protocols are used to declare a list of methods that are used or may be implemented in any other class.

Protocols are declared with the @protocol directive. They have no curly brackets with variables since they cannot have any variables associated with them.

@protocol ProtocolName

//method declarations

– (void) someMethod;

@endb

Protocol method can be marked as required to implement, or as optional to implement. Required methods are marked with the @required keyword, and optional methods are marked with the @optional keyword. If there is no keyword specified, the default is @required.

@protocol MyProtocol

– (void) thisMethodIsRequired;
– (void) thisMethodIsAlsoRequired;

@optional
– (void) thisMethodIsOptional;
– (void) thisMethodIsAlsoOptional;

@required
– (void) thisIsARequiredMethod;

@end

A protocol can implement another protocol in a fashion similar to inheritance with classes, but there is no hierarchy like there are in classes. For example, the MyProtocol could be written in this way instead:

@protocol MyProtocol <SomeProtocol, AnotherProtocol>

@end

@protocol SomeProtocol

– (void) thisMethodIsRequired;

@optional
– (void) thisMethodIsOptional;

@required
– (void) thisIsARequiredMethod;

@end

@protocol AnotherProtocol

– (void) thisMethodIsAlsoRequired;

@optional
– (void) thisMethodIsAlsoOptional;

@end

As shown in this example, protocol’s method declaration can be empty. This may be used to group certain objects based on any criteria that suits the application.

The identity of the class that implements a protocol is not important. The important thing is to find out whether a class of interest implements a protocol or not. If a class implements a protocol then it conforms to that protocol:

– (BOOL) objectConformToSomeProtocol: (id) anObject
{
if ( [anObject conformsToProtocol: @protocol (SomeProtocol)] )
return YES;
else
return NO;
}

The usage of protocols is optional in Objective-C, but they are a very useful tool.

Protocols are useful in many situations, e.g. to declare methods for others to implement, especially when a project is divided among many programmers who are all expected to implement the same group of methods, but may have different implementations.

The example shows a protocol called ProtocolForAllToImplement stored in SomeProtocol.h file.

// SomeProtocol.h

@protocol ProtocolForAllToImplement

– (int) getA;
– (int) getB;

@optional
– (void) printSum;

@required
– (int) sum;

@end

The protocol is implemented into the class interface as shown below.

//SomeClass.h

#import “SomeProtocol.h”

@interface SomeClass : NSObject <ProtocolForAllToImplement>
{

//no variables declared

}

//no methods declared

@end

No methods are declared in the class interface, but methods of the protocols are expected to be implemented, at least the three required ones (getA, getB and sum). The method printSum is optional and does not need to be implemented.

Those methods may be implemented as:

// SomeClass.m

#import “SomeClass.h”

@implementation SomeClass

– (int) getA
{
return A;
}

– (int) getB
{
return B;
}

– (int) sum
{
return [self getA] + [self getB];
}

– (void) printSum
{
printf(“%d”, [self sum]);
}

@end

In some other class that implements the same protocol ProtocolForAllToImplement, the implementation of the declared methods may be different. Protocol is used to declare methods, not the way they are implemented.

Protocols are also very useful for declaring the interface of an object while hiding its class.

id <SomeProtocol>  anObject;

The class of anObject is unknown at this point, but certain methods implemented by this object are known because of the protocol.

Protocols are also very useful for finding similarities among classes that are not hierarchically related because classes in unrelated branches of the inheritance hierarchy might be typed alike because they conform to the same protocol.

SomeClass1 *anObject1;
SomeClass2 *anObject2;

If a class SomeClass1 and a different class SomeClass2 implement the same protocol, they might be typed alike because they conform to the same prtocol.