Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can assign a variable of type <code>id</code> to any object type. If you know that it must implement a given protocol you can assign it to a variable of that type within the method and invoke the method on that variable.</p> <p>As a design point, I would say that it is better to make the protocol explicit and externalise it to the caller so that the compiler can do type checking properly. Some parts of Apple's code are better than others at this: from what you say GameKit is very much at the unhelpful end of things.</p> <p>Defining a category is not what you want to do, because that tells the compiler that you will add the method to every NSObject.</p> <p>If you have this protocol:</p> <pre><code>@protocol YourProtocol &lt;NSObject&gt; - (UIViewController *)viewControllerWithIdentifier:(NSString *)identifier; @end </code></pre> <p>And define your method as:</p> <pre><code>- (void)requestViewControllerWithIdentifier:(NSString *)identifier fromObject:(id &lt;YourProtocol&gt;)object; </code></pre> <p>It will probably do what you want. It may not be strictly necessary in this case but it's usually a good idea to have your protocol extend the <code>NSObject</code> protocol (as above) so that you can call useful stuff like <code>-retain</code>, <code>-release</code>, <code>-autorelease</code>, and <code>-respondsToSelector</code>. The alternative of declaring the method as follows prevents the user from using an <code>NSProxy</code>-rooted object as the <code>object</code> parameter.</p> <pre><code>- (void)requestViewControllerWithIdentifier:(NSString *)identifier fromObject:(NSObject &lt;YourProtocol&gt; *)object; </code></pre> <p>If it's just for your own use and you aren't using proxy objects this can be quite convenient, but you should avoid it in public APIs.</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. 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