Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Informal Protocol In objective-C?

Tags:

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.

like image 753
itsaboutcode Avatar asked Jan 05 '10 23:01

itsaboutcode


People also ask

What is delegate in Objective-C?

A delegate is an object that acts on behalf of, or in coordination with, another object when that object encounters an event in a program. The delegating object is often a responder object—that is, an object inheriting from NSResponder in AppKit or UIResponder in UIKit—that is responding to a user event.

What is the use of category in Objective-C?

Categories provide the ability to add functionality to an object without subclassing or changing the actual object. A handy tool, they are often used to add methods to existing classes, such as NSString or your own custom objects.


1 Answers

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

like image 149
bbum Avatar answered Nov 16 '22 00:11

bbum