Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Ordinarily, when you declare an object as <code>id</code> it counts an an "any" object (meaning that Objective-C will let you invoke any method from any class or protocol on the <code>id</code> without warning).</p> <p>However, when you declare an object as <code>id&lt;SomeProtocol&gt;</code>, the meaning changes. In this case, you are instead saying: I will only invoke <code>SomeProtocol</code> methods on this object.</p> <p>The method:</p> <pre><code>- (void)release; </code></pre> <p>is declared in the <code>NSObject</code> protocol but you have explicitly stated: I will only invoke <code>MyProtocol</code> methods. So the compiler gives you a warning to tell you that you've broken your own promise.</p> <p>Therefore, instead of:</p> <pre><code>id&lt;MyProtocol&gt; reference; </code></pre> <p>you should actually declare:</p> <pre><code>id&lt;MyProtocol, NSObject&gt; reference; </code></pre> <p>or:</p> <pre><code>NSObject&lt;MyProtocol&gt; reference; </code></pre> <p>since <code>NSObject</code> (the class) implements <code>NSObject</code> (the protocol).</p> <p>or:</p> <pre><code>id reference; </code></pre> <p>which is the broadest of the lot: let me invoke anything on this object and never complain.</p> <p>You can also (as Barry Wark suggested) have <code>MyProtocol</code> include the <code>NSObject</code> protocol -- although from a design perspective, you normally only do this if implementing <code>MyProtocol</code> necessarily means using <code>NSObject</code>. Normally, we only do this if <code>NSObject</code> and <code>MyProtocol</code> are linked heritarily or semantically.</p> <hr> <p>A little information about the <code>NSObject</code> protocol:</p> <p>Everything you invoke retain/release/autorelease upon must implement this protocol. As you can infer from this: basically everything implements the <code>NSObject</code> protocol (even though a few things don't descend from the <code>NSObject</code> base class).</p> <p>Another quick clarification: <code>NSObject</code> (the class) and <code>NSObject</code> (the protocol) are not reimplementations of the same API. They are split as follows:</p> <ul> <li><p>NSObject (protocol) implements everything required to handle/inspect an existing object in a generic sense (retain/release, isEqual, class, respondsToSelector: etc).</p></li> <li><p>NSObject (class) implements less generic methods: construction/destruction, thread integration, scripting integration.</p></li> </ul> <p>So in most senses, the protocol is the more important of the two. Remeber that the class includes the protocol so if you descend from NSObject, you get both.</p>
    singulars
    1. This table or related slice is empty.
    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. 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.
    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