Introduction to Protocols

What follows is a quick introduction to working with protocols. This is good background information to understand as protocols are common in various Cocoa frameworks. A protocol is means to define a list of required and/or optional methods that a class implements. If a class adopts a protocol, it must implement all required methods in the protocols it adopts.

Cocoa uses protocols to support interprocess communication through Objective-C messages. In addition, since Objective-C does not support multiple inheritance, you can achieve similar functionality with protocols, as a class can adopt more than one protocol.

A good example of a protocol is NSCoding, which has two required methods that a class must implement. This protocol is used to enable classes to be encoded and decoded, that is, archiving of objects by writing to permanent storage.

@protocol NSCoding
- (void)encodeWithCoder:(NSCoder *)aCoder;
- (id)initWithCoder:(NSCoder *)aDecoder;
@end

To adopt a protocol, enclose the name of the protocol in <> as below:

// Interface
@interface SomeClass : NSObject <NSCoding> 
{
  ...
}
 
// Implementation
@implementation SomeClass
 
-(void)encodeWithCoder:(NSCoder *)aCoder
{
  ...
}
 
-(id)initWithCoder:(NSCoder *)aDecoder
{
  ...
}

Defining a Protocol

You can create both required an optional methods within a protocol. What follows is a definion of a protocol named ‘Fubar’:

@protocol Fubar
- (BOOL)send:(id)data;
- (id)receive;
@optional
- (int)status;
@end

To use the protocol, as with the example above, specify the protocol in the interface and write the required methods in the class implementation:

// Interface
@interface AnotherClass : NSObject <Fubar> 
{
  ...
}
 
// Implementation
@implementation AnotherClass
 
- (BOOL)send:(id)data
{
  ...
}
 
- (id)receive
{
  ...
}
 
// Optional methods
- (int)status
{
  ...
}
 
@end

If you are from a Java background, protocols should look familiar as they are analogous to an interface.

  1. So, a question if you don’t mind. You said that protocols are analogous interfaces which is what I thought.

    Since this is true, does that mean that the polymorphism works the same way on Protocols as they do as Java Interfaces? Using your Fubar example, could I do the following:


    Fubar *fubar = [[AnotherClass alloc] init];

    int RetVal = [fubar status];

    [fubar release]; //Also, would I have access to release ?

    Please excuse the quick example and I’m sorry if the Objective-C isn’t quite right. I’m still learning it…

    Thank you,
    Frank

  2. [Regarding your question, Frank…]

    Your example would attempt to make a Fubar class pointer called fubar and point it at AnotherClass instance, which shouldn’t work as no Fubar class was defined

    To explicitly check if the class conforms to Fubar you can use conformsToProtocol:
    if ([AnotherClass conformsToProtocol;@protocol(Fubar)]) {
    AnotherClass *fubar = [[AnotherClass alloc] init];
    }

    Another option, use the object placeholder “id” along with , to typecheck that you want any object that conforms to Fubar protocol:
    id *fubar = [[AnotherClass alloc]init];

    If AnotherClass doesn’t conform to Fubar then you will receive a compiler error

  3. Another option, use the object placeholder “id” along with ‹Fubar›, to typecheck that you want any object that conforms to Fubar protocol:
    id ‹Fubar› *fubar = [[AnotherClass alloc] init];

  4. dax,

    Thank you very much for taking the time to answer my question. It is clear that I don’t understand this part of Objective-C. Time to go to the book store. Again, thank you.

    Regards,
    Frank

  5. dax,

    I just spent some time fighting with this, trying to get the syntax right amid myriad unhelpful compiler messages. Correct me if I’m wrong (I don’t have my code in front of me right now), but I believe the “*” in “id<Fubar> *fubar” should not be there, since id is already by definition a pointer.

  6. I am seriously fascinated along with your writing skills as well as with the format on your blog. Is it a paid out design or do you personalize it all by yourself? In either case continue great top quality writing, it really is uncommon to see a good web site just like this these days..

Comments are closed.