title: How to correctly implement subclass constructor in Objective-C
body: <p>I have a base class who's constructor sets the objects state given some external data:</p>
  
  <pre><code>@implementation XMlContent
  
       - (id)initWithParser:(NSXMLParser *)xmlParser {
          self = [super init];
          if (self) {
              if(xmlParser != nil){
              _documentFormat = @"xml";
              _xmlParser = xmlParser;
              xmlParser.delegate = self;
              } else{
                  return nil;
              }
          }
          return self; }
  </code></pre>
  
  <p>I have a subclass of this object and I have overridden this constructor like so (probably wrong):</p>
  
  <pre><code>@implementation AtomContent
        - (id)initWithParser:(NSXMLParser *)xmlParser
      {
          self = [super init];
          if (self) {
              if(xmlParser != nil){
                  self.xmlParser = xmlParser;
                  self.xmlParser.delegate = self;
                  self.documentFormat = @"atom";
              } else{
                  return nil;
              }
          }
          return self;
      }
  </code></pre>
  
  <p>The issue I have is that when creating an instance of the subclass (AtomContent in this case) I get back an instance of the superclass and thus superclass methods are called rather than those in the subclass. Looking at my subclass constructor its obvious why. My question is how do I property implement the subclass constructor so the super class is init'd but references to 'self' are to the subclass? Should I not override the constructor but rather use setters to set object state?</p>
  
  <p><strong>Corrections</strong></p>
  
  <blockquote>
    <p>I get back an instance of the superclass and thus superclass</p>
  </blockquote>
  
  <p>-No. Specifically my issue is that base class implementation is used (the NSXMLParser delegate methods) rather than the subclass'. I am, indeed, returned an instance of the subclass.</p>
  
  <blockquote>
    <p>constructor</p>
  </blockquote>
  
  <p>-Yeah ok.. "Initializer"</p>
  
  <p>Thanks! </p>