Unofficial Protocol In objective-C?

I was wondering if anyone can explain what unofficial protocols are in Objective-C? I try to figure this out on apple documentation and some other books, but my head is still spinning, so I will be very grateful if anyone can explain the example.

Thank.

+50
objective-c
Jan 05 '10 at 23:59
source share
7 answers

An informal protocol was, as Junnathan said, typically a category declared in NSObject without an appropriate implementation (most often it was a rare one that provided dummy implementations in NSObject).

Starting with 10.6 (and in the iPhone SDK), this template is no longer used. In particular, what was stated in 10.5 (and earlier) as follows:

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

Now declared as:

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

That is, informal protocols are now declared as @protocol using the @optional set of methods.

In any case, the unofficial protocol is a collection of method declarations by which you can optionally use methods to change behavior. Typically, but not always, method implementations are provided in the context of delegation (a table view data source must implement several required methods and can, for example, implement some additional methods).

+58
Jan 6
source share

One common example of informal protocols is callback definition. Suppose you are using a library that allows you to download something in the background. This library allows you to register a callback object that will be called upon completion.

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

When the download is complete, it will call a specific method for 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 the category. As a result, all objects in the system will respond to the downloadComplete: method, although they will not do anything in response to this method by default. Classes that override the downloadComplete: method can provide more useful functionality.

So far, you can do the same with the formal protocol. However, informal protocols allow the use of optional methods. A class that implements a formal protocol must provide an implementation for each method in the protocol. A class that implements an unofficial protocol can omit the implementation for any method - it already inherited the implementation from NSObject .

Because Objective-C 2.0, formal protocols may contain optional methods. In addition, Apple may opt out of informal protocols for the new APIs - UIAccelerometerDelegate is a formal protocol.

+9
Jan 06
source share

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

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

informal protocol usually declared by the categories of the NSObject class, as it is widely associated with the name of the method with any class that inherits from NSObject .

Because all classes inherit from the root class, methods arent limited to any part of the inheritance hierarchy. (One could also declare a informal protocol as a category of another class, to limit it to a specific branch of the inheritance hierarchy, but there is little reason for this).

When used to declare a protocol, the category interface does not have an appropriate implementation. Instead, classes that implement the protocol again declare methods in their interface files and define them along with other methods in their implementation files.

+5
Jan 05 2018-12-12T00:
source share

Informal protocols are a way to add optional methods to an object using a category.

Therefore, one doubt may arise

Will it be an unofficial protocol if there are any additional methods for the protocol itself?

The answer is no.

If methods are declared in a protocol and are said to correspond to a class without using a category, then this is a formal protocol.

Note:

In object c 2.0, additional methods were introduced in the protocol, so before that the goal was achieved through the informal protocol Ie through the category.

Category:

This is a language level function that should be an alternative to the aka inheritance subclass.

I hope he sheds light of lime on this.

+5
Apr 09 '13 at 19:41
source share

The entire unofficial protocol is a category in some class (often NSObject ) that indicates the interface for the protocol. AppKit uses this for its delegation.

The subclass you write can implement these methods. The difference between this and the formal protocol is that formal protocols are declared using the @protocol ... @end notation. It is not checked whether the class implements this unofficial protocol.

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

+4
Jan 06 '10 at 0:10
source share

Based on Jonathan Sterling's answer, can I say that the following code is an unofficial protocol?

Apple Documentation:

"When used to declare a protocol, the category interface does not have an appropriate implementation. Instead, classes that implement the protocol again declare methods in their 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; } 
+3
Jan 6
source share

an unofficial protocol defines what methods an object should understand. This is called "according to protocol." Compliance with the protocol is independent of the class hierarchy. When declaring a pointer to store a reference to an object, you can determine which protocols this object should correspond to. If you write code that assigns an object that does not conform to all the required protocols, you will get a warning at compile time. Informal protocols help you rely on a set of methods that an object understands. You do not need to call isKindOfClass: or it answers: in your code, to check if the transferred objects are passed, suitable for your processing. Protocols are a kind of aspect-oriented programming.

+1
Jan 06 2018-10-06T00:
source share



All Articles