Note that there are some explanatory texts on larger screens.

plurals
  1. PODo I have to specify a protocol if I want my class to have a delegate?
    primarykey
    data
    text
    <p>I'm new to Objective-C and development on Apple's platforms, but hopefully I can frame this question in an understandable way regardless :)</p> <p>I want to parse an XML feed for my iPhone app, and I decided instead of shoving all of the delegation methods from an instance of NSXMLParser into my view controller, I'd wrap this up inside of a FeedParser class. After reading a few docs and example code, here's what I came up with:</p> <pre><code>@protocol FeedParserDelegate &lt;NSObject&gt; - (void)parserDidLoadEpisodes:(NSArray *)episodes; @end @interface FeedParser : NSObject { id &lt;FeedParserDelegate&gt; delegate; } @property (nonatomic, assign) id &lt;FeedParserDelegate&gt; delegate; - (id)initWithURL:(NSURL *)url delegate:(id&lt;FeedParserDelegate&gt;)theDelegate; @end </code></pre> <p>Simple. Delegates of my FeedParser object just have to implement <code>parserDidLoadEpisodes</code>.</p> <p>However, as I started actually making my <code>FeedParser</code> class use <code>NSXMLParser</code>, I realized that I didn't have to specify that <code>FeedParser</code> implements a protocol for <code>NSXMLParser</code> -- I could just implement the delegate methods that I wanted to do something with. I think I've noticed this with other classes that follow the delegate pattern, too, but not all.</p> <p>So why wouldn't I too just not bother with specifying a formal protocol for my <code>FeedParser</code> class? It would cut away some perhaps unnecessary code. I guess the question is: why would I <em>want</em> to create a formal protocol instead of just doing something like just checking to see if the method is implemented on the delegate with <code>respondsToSelector</code>? Is it only so that the compiler will issue nice warnings if a required delegate method isn't implemented?</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.
 

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