Informal Protocol In objective-C?

后端 未结 7 624
旧巷少年郎
旧巷少年郎 2020-11-28 03: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 spi

相关标签:
7条回答
  • 2020-11-28 04:42

    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.

    0 讨论(0)
  • 2020-11-28 04:43

    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.

    0 讨论(0)
  • 2020-11-28 04:43

    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;
    }
    
    0 讨论(0)
  • 2020-11-28 04:47

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

    0 讨论(0)
  • 2020-11-28 04:51

    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.

    0 讨论(0)
  • 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).

    0 讨论(0)
提交回复
热议问题