Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy must my ObjC implementation class #import the _interface_ that implements a protocol?
    primarykey
    data
    text
    <p>I don't understand why I have to do the following in Objective-C. I'm interested in discovering why, in a general sense (i.e. why does the language/compiler force me to do it this way), and maybe discover alternatives to the implementation I have come up with:</p> <p>I have a protocol:</p> <pre><code>// (file: MyObserver.h) @class TheObserved; @protocol MyObserver &lt;NSObject&gt; - (void)itWasObserved:(TheObserved *)observedInstance; @end </code></pre> <p>There is an interface, 'TheObserved' that (cyclically) uses the protocol:</p> <pre><code>// (file: TheObserved.h) @protocol MyObserver; @interface TheObserved : NSObject @property id &lt;MyObserver&gt; myObserver; - (void)lookAtThisData:(NSString *)someData withObserver:(id &lt;MyObserver&gt;)observer; @end </code></pre> <p>This is to say, as you can see, the protocol has a message which takes an instance of the interface, and the interface has a method which takes an instance that implements the protocol.</p> <p>In my implementation of the interface:</p> <pre><code>// (file: TheObserved.m) #import "TheObserved.h" #import "MyViewController.h" // &lt;-- note this import @implementation TheObserved @synthesize myObserver; - (void)lookAtThisData:(NSString *)someData withObserver:(id &lt;MyObserver&gt;)observer { self.myObserver = observer; // some asynchronous looking at the data is done here. } - (void)asynchCallback:(NSData *) data { // check if the data is as we expect, if so .. [self.myObserver itWasObserver: self] } @end </code></pre> <p>MyViewController.h implements the protocol:</p> <pre><code>// (file: MyViewController.h) #import &lt;UIKit/UIKit.h&gt; #import "OWLMorphDataObserver.h" @interface MyViewController : UITableViewController &lt;MyObserver&gt; @end </code></pre> <p>And in its implementation:</p> <pre><code>// (file: MyViewController.m) #import "TheObserved.h" @interface MyViewController () { @property TheObserved *observed; @end @implementation MyViewController @synthesize observed; - (void) aMethod:(NSString *)dataString { [self.observed lookAtThisData:dataString withObserver:self] } @end </code></pre> <p>My question is: why do, in my Observed class, I need to #import the "MyViewController.h" which implements the @protocol even though the concrete implementation is never explicitly referenced in the Observed class? If I don't, I get and compilation error: </p> <pre><code>no known instance method for selector 'lookAtThisData:' </code></pre> <p>The issue here is, of course that I want to have multiple different view controllers implement this protocol. So why am I forced to import one of them to get it to compile?</p> <p>Is there another way I can structure this code to get the desired effect without a importing a concrete implementation of the protocol in the class that wants to use it?</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.
 

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