Difference between revisions of "Protocol"

From GNUstepWiki
Jump to navigation Jump to search
 
(Formal / Informal Protocol)
Line 1: Line 1:
Protocols are used to define the interface for a set of methods, but not the implementation or any data members.
+
== Formal Protocols ==
 +
 
 +
Formal Protocols are used to define the [[Interface]] for a set of methods, but not the implementation or any data members.
  
 
This is useful for defining an interface which objects will need to conform to in order to take advantage of some service or other.
 
This is useful for defining an interface which objects will need to conform to in order to take advantage of some service or other.
 +
 +
Example:
 +
 +
    @protocol MyFormalProtocol
 +
   
 +
    -(void) someMethod;
 +
    -(id) someOtherMethod;
 +
   
 +
    @end
 +
 +
 +
== Informal Protocols ==
 +
 +
Informal Protocols are constructs that allow you to share a common [[Interface]] between two classes without them having to inherit from any specific object (the notable exception being NSObject).
 +
 +
They are generally made with a category on NSObject that has no implementation associated with it.
 +
 +
Example:
 +
 +
    @interface NSObject (MyInformalProtocol)
 +
   
 +
    - (id) someMethod;
 +
    - (void) anotherMethod;
 +
   
 +
    @end

Revision as of 14:08, 30 August 2005

Formal Protocols

Formal Protocols are used to define the Interface for a set of methods, but not the implementation or any data members.

This is useful for defining an interface which objects will need to conform to in order to take advantage of some service or other.

Example:

   @protocol MyFormalProtocol
   
   -(void) someMethod;
   -(id) someOtherMethod;
   
   @end


Informal Protocols

Informal Protocols are constructs that allow you to share a common Interface between two classes without them having to inherit from any specific object (the notable exception being NSObject).

They are generally made with a category on NSObject that has no implementation associated with it.

Example:

   @interface NSObject (MyInformalProtocol)
   
   - (id) someMethod;
   - (void) anotherMethod;
   
   @end