Informal Protocol In objective-C?
Asked Answered
F

7

55

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.

Fess answered 5/1, 2010 at 23:59 Comment(0)
A
62

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

Alcoholism answered 6/1, 2010 at 0:29 Comment(0)
P
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.

Pelota answered 6/1, 2010 at 0:24 Comment(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.)Policyholder
O
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..

Oliguria answered 9/4, 2013 at 19:41 Comment(1)
Thanks for that "Note :" gave me complete picture :-)Bronchoscope
M
6

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.

Mccahill answered 5/1, 2012 at 12:52 Comment(0)
T
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).

Tropine answered 6/1, 2010 at 0:10 Comment(1)
So its about "Subclassing" thing and then implement them? I was trying <protocol/category> syntax.Fess
F
4

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;
}
Fess answered 6/1, 2010 at 0:29 Comment(4)
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];.Alcoholism
Thanks bbum, i am not interested if its used too much, but i really wanted to understand the whole concept!Fess
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!Alcoholism
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.Fess
T
2

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.

Tolentino answered 6/1, 2010 at 0:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.