What is Swift’s
In Xcode, go to
File > New > Project,
select “Cocoa application”,
and call it “LookMaNoNSApplicationMain”.
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 class has the
which causes your program to read the
and construct the window and menu based on its contents.
All of those menu bar items are in that
which is several hundred lines long.
One piece of complexity is the
@NSApplicationMain is part of the Swift language, and is poorly documented.
@NSApplicationMain is a macro:
it rewrites your program at compile time.
To understand what rewriting
let’s manually rewrite it.
First, remove the
@NSApplicationMain annotation in your
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.
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.
and assigns this
NSApplication object to
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
Notice the documentation refers to a
even though in Swift there is none!
The equivalent is the
main.swift instantiates your
and assigns it as the
You can now see why the default project chooses to call the class
it is set as the
.delegate on an
main.swift calls the function
Don’t confuse this with the
@NSApplicationMain annotation in Swift!
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
I wrote this because I felt like it. This post is not associated with my employer.