Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A protocol, declared with the (<code>@protocol</code> syntax in Objective-C) is used to declare a set of methods that a class "adopts" (declares that it will use this protocol) will implement. This means that you can specify in your code that, "you don't care which class is used as long as it implements a particular protocol". This can be done in Objective-C as follows:</p> <p><code>id&lt;MyProtocol&gt; instanceOfClassThatImplementsMyProtocol;</code></p> <p>If you state this in your code, then any class that "conforms" to the protocol <em>MyProtocol</em> can be used in the variable <em>instanceOfClassThatImplementsMyProtocol</em>. This means that the code that uses this variable knows that it can use whichever methods are defined in <em>MyProtocol</em> with this particular variable, regardless of what class it is. This is a great way of avoiding the inheritance design pattern, and avoids tight coupling.</p> <p>Delegates are a use of the language feature of protocols. The <a href="http://en.wikipedia.org/wiki/Delegation_pattern" rel="nofollow noreferrer">delegation design pattern</a> is a way of designing your code to use protocols where necessary. In the Cocoa frameworks, the delegate design pattern is used to specify an instance of a class which conforms to a particular protocol. This particular protocol specifies methods that the delegate class should implement to perform specific actions at given events. The class that uses the delegate knows that its delegate coforms to the protocol, so it knows that it can call the implemented methods at given times. This design pattern is a great way of decoupling the classes, because it makes it really easy to exchange one delegate instance for another - all the programmer has to do is ensure that the replacement instance or class conforms to the necessary protocol (i.e. it implements the methods specified in the protocol)!</p> <p>Protocols and delegates are not restricted only to Objective-C and Mac/iOS development, but the Objective-C language and the Apple frameworks make heavy use of this awesome language feature and design pattern.</p> <p><strong>Edit:</strong></p> <p>Here's an example. In the UIKit framework of Cocoa Touch, there is a <em>UITextFieldDelegate</em> protocol. This protocol defines a series of methods that classes which are delegates of a <em>UITextField</em> instance should implement. In other words, if you want to assign a delegate to a <em>UITextField</em> (using the <code>delegate</code> property), you'd better make sure that this class conforms to <em>UITextFieldDelegate</em>. In fact, because the delegate property of <em>UITextField</em> is defined as:</p> <p><code>@property(nonatomic, weak) id&lt;UITextFieldDelegate&gt; delegate</code></p> <p>Then the compiler will give warnings if you assign a class to it that doesn't implement the protocol. This is really useful. You have to state that a class implements a protocol, and in saying that it does, you're letting other classes know that they can interact in a particular way with your class. So, if you assign an instance of <em>MyTextFieldDelegateClass</em> to the <code>delegate</code> property of <em>UITextField</em>, the <em>UITextField</em> <strong>knows</strong> that it can call some particular methods (related to text entry, selection etc.) of your <em>MyTextFieldDelegateClass</em>. It knows this because <em>MyTextFieldDelegateClass</em> has said that it will implement the <em>UITextFieldDelegate</em> protocol.</p> <p>Ultimately, this all leads to much greater flexibility and adaptability in your project's code, which I'm sure you'll soon realise after using this technology! :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload