Note that there are some explanatory texts on larger screens.

plurals
  1. PORequiring the presence of a method in an id
    text
    copied!<h2>The Situation</h2> <p>My custom controller class has the following method:</p> <pre><code>- (void)requestViewControllerWithIdentifier:(NSString *)identifier fromObject:(id)object; </code></pre> <p>This causes <code>object</code> to receive this message:</p> <pre><code>- (UIViewController *)viewControllerWithIdentifier:(NSString *)identifier; </code></pre> <hr> <h2>The Problem</h2> <p>Right now, <code>object</code> is just an <code>id</code> so there's no guarantee that it actually implements that method, and I get a compiler warning when I send the message.</p> <hr> <h2>The Solution</h2> <p>I came up with two solutions myself:</p> <ol> <li>Use a protocol.</li> <li>Make <code>id</code> an <code>NSObject</code> and create a category for <code>NSObject</code>.</li> </ol> <p>They are both fine solutions probably and I don't mind choosing one of them, but...</p> <hr> <h2>The Question</h2> <p>...I noticed Apple is doing something odd in their GameKit API. <code>GKSession</code> has the following method:</p> <pre><code>- (void)setDataReceiveHandler:(id)handler withContext:(void *)context </code></pre> <p><code>handler</code> is just an <code>id</code>, but Apple actually requires it to implement this method:</p> <pre><code>- (void) receiveData:(NSData *)data fromPeer:(NSString *)peer inSession: (GKSession *)session context:(void *)context; </code></pre> <p>Without making use of any protocol or category! I'm wondering how and why would they do this? Why don't use a protocol? Do they enforce the method in some other way? If I were to do this, how can I suppress the compiler warning?</p>
 

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