54

I was wondering if someone can explain what is informal protocols in Objective C? I try to understand it on apple documentation and some other books but my head is still spinning so i will really appreciate that if someone can explain with example.

Thanks.

itsaboutcode
  • 24,525
  • 45
  • 110
  • 156

7 Answers7

61

An informal protocol was, as Jonnathan said, typically a category declared on NSObject with no corresponding implementation (most often -- there was the rare one that did provide dummy implementations on NSObject).

As of 10.6 (and in the iPhone SDK), this pattern is no longer used. Specifically, what was declared as follows in 10.5 (and prior):

@interface NSObject(NSApplicationNotifications)
- (void)applicationWillFinishLaunching:(NSNotification *)notification;
...
@interface NSObject(NSApplicationDelegate)
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender;
...

Is now declared as:

@protocol NSApplicationDelegate <NSObject>
@optional
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender;
...
- (void)applicationWillFinishLaunching:(NSNotification *)notification;
...

That is, informal protocols are now declared as @protocols with a bunch of @optional methods.

In any case, an informal protocol is a collection of method declarations whereby you can optionally implement the methods to change behavior. Typically, but not always, the method implementations are provided in the context of delegation (a table view's data source must implement a handful of required methods and may optionally implement some additional methods, for example).

Etan
  • 17,014
  • 17
  • 89
  • 148
bbum
  • 162,346
  • 23
  • 271
  • 359
9

One of the common examples given of informal protocols is to define callbacks. Suppose you are using a library that lets you download something in the background. This library lets you register a callback object to be called when complete.

- (void)download:(NSURL*)url whenComplete:(id)callback

When the download is complete, it will call a particular method on your callback object:

- (void)downloadComplete:(NSURL*)url

Of course, there is no guarantee that your callback object actually implements this method. Informal protocols provide trivial implementations of these methods on NSObject, using a category. As a result, all objects in the system will respond to the downloadComplete: method, though they will do nothing in response to that method by default. Classes that override the downloadComplete: method can provide more useful functionality.

So far, you can accomplish the same thing with a formal protocol. However, informal protocols allow you to have optional methods. A class that implements a formal protocol must provide an implementation for every method in the protocol. A class implementing an informal protocol can omit implementation for any method - it has already inherited an implementation from NSObject.

Since Objective-C 2.0, formal protocols can contain optional methods. In addition, Apple might be moving away from informal protocols for new APIs - UIAccelerometerDelegate is a formal protocol.

Daniel Yankowsky
  • 6,956
  • 1
  • 35
  • 39
  • 1
    The use of the term `callback` is potentially confusing here, especially in a world where blocks exist. What you're talking about is delegation. (I realize that this answer probably made more sense in 2010.) – Wayne Nov 21 '14 at 20:07
7

Informal protocols are a way to add optional methods to an object by means of category.

So one doubt may arise

Will it become informal protocol if there are any optional methods on protocol itself?

The answer is no.

If the methods are declared in protocol and it is said to be conforming to a class without any usage of category then it's formal protocol.

Note:

Optional methods in protocol were introduced in objective c 2.0 so before that the purpose was achieved through informal protocol I.e by means of category.

Category:

It is a language level feature meant to be alternative for sub classing aka inheritance.

I hope it sheds some lime light on this..

Durai Amuthan.H
  • 31,670
  • 10
  • 160
  • 241
5

We define an informal protocol by grouping the methods in a category declaration,

@interface NSObject ( MyXMLSupport )
- initFromXMLRepresentation:(NSXMLElement *)XMLElement;
- (NSXMLElement *)XMLRepresentation;
@end

Informal protocols are typically declared as categories of the NSObject class, Since that broadly associates the method names with any class that inherits from NSObject.

Because all classes inherit from the root class, the methods aren’t restricted to any part of the inheritance hierarchy. (It would also be possible to declare an informal protocol as a category of another class to limit it to a certain branch of the inheritance hierarchy, but there is little reason to do so).

When used to declare a protocol, a category interface doesn’t have a corresponding implementation. Instead, classes that implement the protocol declare the methods again in their own interface files and define them along with other methods in their implementation files.

Arun
  • 3,406
  • 4
  • 30
  • 55
4

All an informal protocol is is a category on some class (often NSObject) that states the interface for the protocol. AppKit uses this a lot for its delegation.

A subclass that you write can implement these methods. The difference between this and a formal protocol is that formal protocols are declared using the @protocol ... @end notation. There is no checking whether a class implements a given informal protocol.

I almost always use formal protocols, but I suppose an informal protocol is useful if you want to provide default behavior (just provide an implementation for your category that can be overridden).

Jonathan Sterling
  • 18,320
  • 12
  • 67
  • 79
3

Based on "Jonathan Sterling" answer, can i say the following code represent informal protocol?

Apple documentation:

"When used to declare a protocol, a category interface doesn’t have a corresponding implementation. Instead, classes that implement the protocol declare the methods again in their own interface files and define them along with other methods in their implementation files."

#import <Foundation/Foundation.h>

@interface Cat1 : NSObject {


}
- (void) simpleMethod;

@end

@implementation Cat1

- (void) simpleMethod
{

    NSLog(@"Simple Method");
}

@end


@interface Cat1 (Cat2) 
- (void) addingMoreMethods;

@end




@interface MYClass : Cat1

@end

@implementation MYClass

- (void) addingMoreMethods
{
    NSLog(@"Testing!");
}
@end

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];


    MYClass *myclass = [[MYClass alloc] init];
    [myclass addingMoreMethods];
    [myclass release];
    [pool drain];
    return 0;
}
itsaboutcode
  • 24,525
  • 45
  • 110
  • 156
  • Sorta. The method declared in *Cat2* is an informal protocol, more or less. The usage, though, is not -- typically, you would see `if([myclass respondsToSelector: @selector(addingMoreMethods)]) [myclass addingMoreMethods];`. – bbum Jan 06 '10 at 00:31
  • Thanks bbum, i am not interested if its used too much, but i really wanted to understand the whole concept! – itsaboutcode Jan 06 '10 at 00:35
  • 3
    The point, though, is that an informal protocol is composed of methods that may optionally be implemented. Thus, the calling site *must* check to see if it is implemented or else the call will crash! – bbum Jan 06 '10 at 00:46
  • Yah you are right, we should check if it respond to. Why? because lets say NSObject has a category named "NSCoderMethods", since i dont know if it implement or not (i dont have its code), so i should always check it. Infact all the category methods should be check if they respond or not using respondsToSelector. – itsaboutcode Jan 06 '10 at 01:01
1

an informal protocol defines which methods an object must understand. This is called "conforming to a protocol". Conforming to a protocol is independant from the class hierarchy. When declaring a pointer to hold the reference to an object you may define to which protocols this object should conform. If you write code that assigns an object which doesn't conform to all the required protocols, you'll get a warning at compile time. Informal protocols help you to rely on a set of methods that an object understands. You don't have to invoke isKindOfClass: or respondsTo: in your code to check wether objects passed in will be suitable for your processing. Protocols are sort of aspect oriented programming.