Professional Web Applications Themes

What is a Protocol? - Mac Programming

COuld someone please clarify what a protocol is in Cocoa and and simple example of how one is used?...

  1. #1

    Default What is a Protocol?

    COuld someone please clarify what a protocol is in Cocoa and and simple
    example of how one is used?



    Eli Guest

  2. #2

    Default Re: What is a Protocol?

    Eli Hughes <edu> wrote:
     

    A protocol is not a Cocoa thing. It is an Objective-C thing. The docs
    are superb; read them. Example:

    <http://developer.apple.com/doentation/Cocoa/Conceptual/ObjectiveC/3o
    bjc_language_overview/chapter_3_section_7.html#//apple_ref/doc/uid/20001
    424/BAJJABHJ>

    m.

    --
    matt neuburg, phd = com, http://www.tidbits.com/matt/
    AppleScript: The Definitive Guide
    http://www.amazon.com/exec/obidos/ASIN/0596005571/somethingsbymatt
    Read TidBITS! It's free and smart. http://www.tidbits.com
    matt Guest

  3. #3

    Default Re: What is a Protocol?

    In article <c802hi$v1c$cac.psu.edu>, Eli Hughes
    <edu> wrote:
     

    You need to read about protocols in the Apple's Objective C manual but
    the central concept is that a protocol declares methods that the
    adoptor of a protocol must implement if it wants to provide the
    functionality that the protocol is supposed to provide. Cocoa uses
    this functionality in many ways to declare methods that you implement
    for Cocoa to call.

    For example, the data source protocols for table views and combo boxes
    permit you to make any class you want to be a data source. Similarly,
    if you have an object that you want to be able to write out using
    encoders and decoders, you implement the NSCoding protocol in your
    class. Once you implement the required methods, they will be called at
    the appropriate time.

    There are two different methods for implementing a protocol and they
    get used slightly differently.

    The first is what is called an "informal protocol". These are simply
    declared as categories typically categories on NSObject which means any
    class which inherits from NSObject can get the functionality simply by
    implementing the methods declared in the protocol. The table data
    source protocol is of this type. It is declared:

    interface NSObject(NSTableViewDelegate)
    ....
    end

    Now to use the protocol, all the user need do, assuming you have
    inherited from NSObject either directly or indirectly, is to implement
    any or all of the protocols methods and, in the case of a table data
    source, set your object as the data source for the view. (Note that
    while as far as the compiler is concerned, you don't have to implement
    all of the protocols methods, there are some which, if you don't, a
    runtime exception will be thrown.

    Informal protocols are very easy to use but they provide little
    language support. They are best for when the implementation of
    particular methods is optional as the compiler will not complain if you
    don't implement the methods. This feature is how most delegate
    functions are implemented.

    The second type of protocol is the so-called "formal protocol". These
    are true protocols as declared by the Objective C language. The
    NSCoding protocol is of this type. It is declared:

    protocol NSCoding

    - (void)encodeWithCoder:(NSCoder *)aCoder;
    - (id)initWithCoder:(NSCoder *)aDecoder;

    end

    To use this protocol, your class must formally adopt it which is done
    in your class declaration:

    interface MyObject : NSObject <NSCoding> {

    }

    end

    Having adopted the protocol, your class now MUST implement ALL of the
    methods in the protocol declaration. In the case of NSCoding, you MUST
    implement both encodeWithCoder and initWithCoder methods; if you don't,
    the compiler will complain.

    Formal protocols have the advantage of strong language support; besides
    getting warnings if you have incompletely implemented the protocol, you
    can test whether whether an object implements a protocol in various
    ways, e.g. by using the conformsTo: method or by using type checking
    (you can statically type an object variable to be not only of a
    particular class but also to adopt a particular protocol:

    id <NSCoding> myObject; // declares my object to be a member of a class
    // adopting the NSCoding protocol.

    As a last point to help you along in reading Cocoa's doentation
    about the informal and formal protocols it declares: you can have a
    class that has the same name as a formal protocol. This doesn't happen
    much but you should know that there is no only a class NSObject (the
    root object in Cocoa from which essentially every other class is
    derived) but also a formal protocol called NSObject which is adopted by
    the class NSObject.

    Spence

    --
    James P. Spencer
    Rochester, MN

    "Badges?? We don't need no stinkin badges!"
    James Guest

Similar Threads

  1. RTMP Protocol
    By the dude881 in forum Macromedia Flash Flashcom
    Replies: 0
    Last Post: July 21st, 05:08 PM
  2. what is ip protocol 47
    By Ankit Shah in forum Windows Networking
    Replies: 0
    Last Post: July 16th, 08:50 PM
  3. Deleted protocol
    By Tim in forum Windows Networking
    Replies: 1
    Last Post: July 8th, 02:37 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139