23

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()?

Omkar
  • 1,108
  • 10
  • 19
  • Possible duplicate of: http://stackoverflow.com/questions/24516250/what-does-uiapplicationmain-mean (this is for iOS' `@UIApplicationMain` but the same applies to `@NSApplicationMain` in OSX) – Alladinian Dec 14 '14 at 12:59

1 Answers1

40

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 a main function that calls the NSApplicationMain function. For example, if your app uses a custom subclass of NSApplication as its principal class, call the NSApplicationMain function instead of using this attribute.

Community
  • 1
  • 1
Matt Gibson
  • 37,886
  • 9
  • 99
  • 128
  • Is there a way to use a different name for the `main.swift`, perhaps there's a compiler option? I'm trying to figure a way for two different targets to use their own `main.swift` entry points, yet, somehow store both in the same folder. – Ian Bytchek Mar 29 '16 at 08:46
  • @IanBytchek If you're using the `@NSApplicationMain` attribute I don't think it'll matter what the file's called, but I'm not on a Mac to check right now. As long as each target only includes one main entry point in its build, I'd think you would be fine storing them in the same folder. If you're having problems, you should probably ask a new question about your specific issue. – Matt Gibson Mar 29 '16 at 08:49
  • My understanding is that `@NSApplicationMain` works only in front of an application delegate, it doesn't compile when put anywhere else. – Ian Bytchek Mar 29 '16 at 08:52
  • @IanBytchek Yup, looks very much like `main.swift` is definitely [a special, specifically-name file](https://developer.apple.com/swift/blog/?id=7) that must be called `main.swift`. Looks to me like you're out of luck if you want to have two different plain Swift apps with their entry point files stored in the same folder. But you should probably ask a separate question on this specifically if you want to be sure. – Matt Gibson Mar 29 '16 at 09:00
  • Yep. That's alright, I'll try living with `@NSApplicationMain` like normal people do. – Ian Bytchek Mar 29 '16 at 09:41