I've created a sample project using Xcode 6 and language as swift.
Xcode created AppDelegate.swift with following code
import Cocoa @NSApplicationMain class AppDelegate: NSObject, NSApplicationDelegate { func applicationDidFinishLaunching(aNotification: NSNotification) { // Insert code here to initialize your application } func applicationWillTerminate(aNotification: NSNotification) { // Insert code here to tear down your application } }
What does @NSApplicationMain mean and where is my main.mm/main.m file with main()?
The NSApplicationMain function sends shared to the principal class to obtain the global app instance ( NSApp )—which in this case will be an instance of your custom subclass of NSApplication . Many AppKit classes rely on the NSApplication class and may not work properly until this class is fully initialized.
Swift is a programming language that is used for building iOS and Mac applications, while Xcode is not a programming language – it is a comprehensive IDE. Xcode is the canvas to write and program the code, while Swift is the actual code that your team is writing.
So “SwiftUI” is the thing that draws buttons and stuff, Swift is the language it is written in, and probably the language that a programmer doing a “let's show a list of flowers, let people tap on them, and see details about them” app uses.
SwiftUI is Apple's brand new framework for building user interfaces for iOS, tvOS, macOS, and watchOS. Apple introduced SwiftUI in 2019 and the framework has been evolving at a rapid pace ever since. Unlike UIKit and AppKit, SwiftUI is a cross-platform framework.
As it mentioned in the release notes:
OS X apps can now apply the @NSApplicationMain attribute to their app delegate class in order to generate an implicit main for the app. This attribute works in the same way as the @UIApplicationMain attribute for iOS apps."
So, basically, it's a way of getting your app bootstrapped and running without having to write some global-level boilerplate in a separate Swift file. In previous versions of Xcode, when you created a new Swift Cocoa project, Xcode would generate a main.swift
file for a new OS X project that looked like this:
import Cocoa NSApplicationMain(Process.argc, Process.unsafeArgv)
And that was the main entry point. Now Xcode doesn't bother generating this file; instead it just tags your app delegate with @NSApplicationMain
and this bootstrapping step is handled behind the scenes.
More info from The Swift Programming Language:
Apply this attribute to a class to indicate that it is the application delegate. Using this attribute is equivalent to calling the
NSApplicationMain
function and passing this class’s name as the name of the delegate class.If you do not use this attribute, supply a
main.swift
file with amain
function that calls theNSApplicationMain
function. For example, if your app uses a custom subclass ofNSApplication
as its principal class, call theNSApplicationMain
function instead of using this attribute.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With