Protocols
As we have interfaces in Java and C#, and like abstract class in C++ similarly in Objective – C we have ‘protocols‘. A protocol is a list of methods that your confirming class promises to implement. It is not a real class, It can only declare methods and cannot hold any data. We can use Protocols to mark classes as having certain capabilities, like the ability to be copied, to be serialized and deserialized, or to act as a data source for some other class. Protocols make it possible for two classes distantly related by inheritance to communicate with each other to accomplish a certain goal. Thus protocols are used as an alternative to the subclassing.
Declaring Protocols
To declare a custom protocol we can use the below syntax. A protocol can be declared in the ‘.h’ file of a class before the interface declaration.
@protocol MyProtocolName //My protocol methods declaration @end
You can mark a class as conforming to above protocol by declaring so in the class’s interface:
@interface SomeObject : NSObject @end
Here the class ‘SomeObject’ conforms to the protocol ‘MyProtocolName‘, thus the class ‘SomeObject’ must have to implement all the methods declared in the conforming protocol.
Protocols are not same as subclassing a class, where your class inherits number of methods and can choose to override some or all of them. Where as, conforming to the protocol means that you must implement all of the required methods that the protocol specifies.
To create an object conforming to the protocol we use the syntax as below:
@property(weak) id someConformingObject;
Here the object ‘someConfirmingObject’ confirming to ‘MyProtocolName’ is of type ‘id‘ , so that it could be an object of any class at the run-time, but it is guaranteed that it implements the methods listed in the protocol.
Example for implementing custom Protocol:
Consider the two classes classA and classB , where classB contains the protocol called ‘MyProtocol‘. Here my classA has the strong reference to the object of classB and it conforms to the protocol ‘MyProtocol‘. In classB we have an object delegateObj of type id conforming to the protocol MyProtocol, this object holds the weak reference to the object of classA. The implementation for the methods in the protocols will be found in classA. Using the delegateObj in classB we can invoke the protocol methods in classA. i.e classB delegates its job to classB.
classA
//classA.h @interface classA :NSObject @end //classA.m #import "classA.h" #import "classB.h" @implementation classA -(void)initClassBObj { classB *objB = [[classB alloc] init]; objB.delegateobj = self; //setting weak referenc of classA to classB object } -(void)myProtocolMethod { //Delegated Task from classB } @end
classB
//classB.h @protocol MyProtocol -(void)myProtocolMethod; @end @interface classB : NSObject @property (weak) id delegateObj; //Holds the weak reference to classA object @end //classB.m #import "classB.h" @implementation classB -(void)delegateTaskToClassA { [self.delegateObj myProtocolMethod]; //This call delegates the task to protocol //method in classA } @end
Other Details on Protocols:
Formal Protocols:
A formal protocol is a set of methods that must be implemented in any conforming class. This can be seen as a certification regarding a class, ensuring that it is able to handle everything that is necessary for a given service. A class can conform to an unlimited number of protocols.
Informal Protocols:
The informal protocol is not really a ‘protocol‘ ,instead it creates no constrains upon the code. But it is ‘informal‘ in nature and contributes for code auto documentation. This lets us to group methods by application field, so that it organize its classes consistently. We use the concept called ‘class category‘ to implement informal protocols.
Recent Comments