title: Is there a main.swift which is equivalent to the @NSApplicationMain annotation?
body: <p>Creating a new Cocoa project in XCode gives me an <code>AppDelegate.swift</code> file which looks like this:</p>
  
  <pre><code>import Cocoa
  @NSApplicationMain
  class AppDelegate: NSObject, NSApplicationDelegate {
      @IBOutlet weak var window: NSWindow!
  }
  </code></pre>
  
  <p><a href="https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html" rel="noreferrer">The <code>@NSApplicationMain</code> attribute is documented here</a> as</p>
  
  <blockquote>
    <p><code>NSApplicationMain</code></p>
    
    <p>Apply this attribute to a class to indicate that it is the application delegate. Using this attribute is equivalent to calling the <code>NSApplicationMain(_:_:)</code> function.</p>
    
    <p>If you do not use this attribute, supply a <code>main.swift</code> file with code at the top level that calls the <code>NSApplicationMain(_:_:)</code> function as follows:</p>
  
  <pre><code>import AppKit
  NSApplicationMain(CommandLine.argc, CommandLine.unsafeArgv)
  </code></pre>
  </blockquote>
  
  <p>The instructions in the documentation do not work: the <code>AppDelegate</code> class is never instantiated. In <a href="https://stackoverflow.com/a/42875049/229792">this answer</a>, <a href="https://stackoverflow.com/users/5044042/vadian">vadian</a> suggests the following contents for <code>main.swift</code>, which work <em>better</em> than the code in the documentation:</p>
  
  <pre><code>import Cocoa
  let appDelegate = AppDelegate()
  NSApplication.shared().delegate = appDelegate
  _ = NSApplicationMain(CommandLine.argc, CommandLine.unsafeArgv)
  </code></pre>
  
  <p>However, this still does not provide the same behavior as <code>@NSApplicationMain</code>. Consider using the above <code>main.swift</code> with the following <code>AppDelegate.swift</code>:</p>
  
  <pre><code>import Cocoa
  class AppDelegate: NSObject, NSApplicationDelegate {
      @IBOutlet weak var window: NSWindow!
      var foo: NSStatusBar! = NSStatusBar.system();
  }
  </code></pre>
  
  <p>The above <code>AppDelegate.swift</code> works with an <code>@NSApplicationMain</code> annotation, but when using the above <code>main.swift</code>, it fails at runtime with the error</p>
  
  <pre><code>Assertion failed: (CGAtomicGet(&amp;is_initialized)), function CGSConnectionByID, file Services/Connection/CGSConnection.c, line 127.
  </code></pre>
  
  <p>I think this <code>is_initialized</code> error means that <code>@NSApplicationMain</code> sets things up so that the <code>AppDelegate</code> is instantiated <em>after</em> some initialization by the <code>NSApplicationMain</code> function. This suggests the following <code>main.swift</code>, which moves the delegate initialization to after the <code>NSApplicationMain</code> call:</p>
  
  <pre><code>import Cocoa
  _ = NSApplicationMain(CommandLine.argc, CommandLine.unsafeArgv)
  let appDelegate = AppDelegate()
  NSApplication.shared().delegate = appDelegate
  </code></pre>
  
  <p>However, this doesn't work <em>either</em>, because <code>NSApplicationMain</code> never returns! The above <code>main.swift</code> is equivalent to the broken suggestion in the documentation, because the latter two lines are dead code.</p>
  
  <p>I therefore think there must be some way to pass a reference to my <code>AppDelegate</code> class as an argument to the <code>NSApplicationMain</code> function, so that Cocoa can do its initialization and then instantiate my <code>AppDelegate</code> class itself. However, I see no way to do this.</p>
  
  <p><strong>Is there a <code>main.swift</code> which provides behavior which is truly equivalent to the <code>@NSApplicationMain</code> annotation? If so, what does that <code>main.swift</code> look like? If not, what is <code>@NSApplicationMain</code> actually doing, and how do I modify it?</strong></p>