Informal Protocol In objective-C?

Objective C

Objective C Problem Overview


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.

Objective C Solutions


Solution 1 - Objective C

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

Solution 2 - Objective C

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.

Solution 3 - Objective C

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

Solution 4 - Objective C

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.

Solution 5 - Objective C

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

Solution 6 - Objective C

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;
}

Solution 7 - Objective C

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionitsaboutcodeView Question on Stackoverflow
Solution 1 - Objective CbbumView Answer on Stackoverflow
Solution 2 - Objective CDaniel YankowskyView Answer on Stackoverflow
Solution 3 - Objective CDurai Amuthan.HView Answer on Stackoverflow
Solution 4 - Objective CAmol ManwatkarView Answer on Stackoverflow
Solution 5 - Objective CJonathan SterlingView Answer on Stackoverflow
Solution 6 - Objective CitsaboutcodeView Answer on Stackoverflow
Solution 7 - Objective CPirmin BraunView Answer on Stackoverflow