title: How does Xcode setup a document based application?
body: <p>I am learning Cocoa and my understanding from reading the documentation is that when an application starts the following happens:</p>
  
  <ol>
  <li>A shared application instance is created.</li>
  <li>The main nib file is looked up from the applications property list so that the application knows which nib to load.</li>
  <li>the run loop is started.</li>
  </ol>
  
  <p>This is fine and makes sense for s single windowed application however I am confused by what xcode does when a <em>document based application</em> is created.</p>
  
  <p>In this case there are two nib files; the first contains the application menu and the second contains the window which represents the <em>NSDocument</em> subclass. when I run the application a new document window is opened automatically.</p>
  
  <p>Based on my understanding of how the application works outlined above I don't understand how my application knows to open the document window once the menu nib has been looked up from the property list. There is no code generated to do this as far as I can see (except for the <em>windowNibName</em> method but where is this called from?)</p>
  
  <p>Can anyone tell me what xcode does differently so that the application knows that it is document based and therefore needs to open a document window?</p>
  
  <p><strong>Update:</strong></p>
  
  <p>What I am trying to understand is how Xcode knows how to do something different if my application is set up as a document based application rather than a single window application. As far as I am aware there is no setting to specify this and Xcode doesn't appear to generate any code to give this different behaviour.</p>
  
  <p>From reading the documents over the last couple of days I <em>think</em> I know how this works but am not sure:</p>
  
  <ol>
  <li>_NSApplication_has a delegate method <em>applicationOpensUntitledFile</em> which is called by the applications delegate.</li>
  <li><em>NSDocumentController</em> is set as the applications delegate by default and the default implementation looks for the presence of the <em>CFBundledTypeInfo</em> to determine if the document is document based or not and responds as is appropriate for the application (I.E. YES for document based application and NO for single window applications).</li>
  <li>The majority of the time when a single window application is created the application delegate is replaced by a custom <em>AppController</em> anyway which usually wont contain a definition for the <em>applicationOpenUntitledFile</em> method as it is not appropriate for the type of application.</li>
  </ol>
  
  <p>Hopefully any Cocoa experts can confirm if my understanding is correct or if I am barking up the wrong tree.</p>