Informal Protocol In objective-C?

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.


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).


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.


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..


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.