Learn more about Russian war crimes in Ukraine.

What is Swift’s @NSApplicationMain annotation?

In Xcode, go to File > New > Project, select “Cocoa application”, and call it “LookMaNoNSApplicationMain”. Run it. You get an empty window which reads “LookMaNoNSApplicationMain” in the title. When you focus the application, you get a menu bar for it along the top. This menu bar contains a whole lot of functionality. You can go View > Enter Full Screen, and the window maximizes. You can go Format > Font > Show Colors, and you get a color picker window. Where did all this functionality come from?!

The default project gives you a MainMenu.xib and an AppDelegate.swift. The AppDelegate class has the @NSApplicationMain annotation which causes your program to read the MainMenu.xib file and construct the window and menu based on its contents. All of those menu bar items are in that MainMenu.xib file, which is several hundred lines long.

One piece of complexity is the @NSApplicationMain annotation. @NSApplicationMain is part of the Swift language, and is poorly documented. Roughly speaking, @NSApplicationMain is a macro: it rewrites your program at compile time. To understand what rewriting @NSApplicationMain does, let’s manually rewrite it.

First, remove the @NSApplicationMain annotation in your AppDelegate.swift. Then create a new file, main.swift, with these contents:

import Cocoa
let myApp: NSApplication = NSApplication.shared()
let myDelegate: AppDelegate = AppDelegate()
myApp.delegate = myDelegate
_ = NSApplicationMain(CommandLine.argc, CommandLine.unsafeArgv)

This is roughly what the @NSApplicationMain annotation does. The file main.swift is special: the file with this name is allowed to have statements at the top level. You should think of the contents of main.swift as being like the main() function in C. Now let’s walk through the statements in it, line-by-line.

First, main.swift runs NSApplication.shared(), and assigns this NSApplication object to myApp. The NSApplication documentation says

Every app must have exactly one instance of NSApplication (or a subclass of NSApplication). Your program’s main() function should create this instance by invoking the shared() class method.

Notice the documentation refers to a main() function, even though in Swift there is none! The equivalent is the main.swift file.

Next, main.swift instantiates your AppDelegate class, and assigns it as the .delegate of myApp. You can now see why the default project chooses to call the class AppDelegate: it is set as the .delegate on an NSApplication.

Finally, main.swift calls the function NSApplicationMain(...). Don’t confuse this with the @NSApplicationMain annotation in Swift! The function NSApplicationMain(...) is the entry point for Cocoa applications. NSApplicationMain(...) never returns; instead, it sets up the UI event loop, and eventually exits using the C exit(...) function.

What can computers do? What are the limits of mathematics? And just how busy can a busy beaver be? This year, I’m writing Busy Beavers, a unique interactive book on computability theory. You and I will take a practical and modern approach to answering these questions — or at least learning why some questions are unanswerable!

It’s only $19, and you can get 50% off if you find the discount code ... Not quite. Hackers use the console!

After months of secret toil, I and Andrew Carr released Everyday Data Science, a unique interactive online course! You’ll make the perfect glass of lemonade using Thompson sampling. You’ll lose weight with differential equations. And you might just qualify for the Olympics with a bit of statistics!

It’s $29, but you can get 50% off if you find the discount code ... Not quite. Hackers use the console!

More by Jim

Tagged . All content copyright James Fisher 2017. This post is not associated with my employer. Found an error? Edit this page.