What does "@UIApplicationMain" mean?
I just created my first Swift project, in the AppDelegate.swift
there is a line above a class declaration - why is it there?!
...
import UIKit
import CoreData
@UIApplicationMain // <- WHY IS IT HERE?
class AppDelegate: UIResponder, UIApplicationDelegate {
...
The @UIApplicationMain
attribute in Swift replaces the trivial main.m
file found in Objective-C projects (whose purpose is to implement the main
function that's the entry point for all C programs and call UIApplicationMain
to kick off the Cocoa Touch run loop and app infrastructure).
In Objective-C, the main (heh) bit of per-app configuration that the UIApplicationMain
function provides is designating one of your app's custom classes as the delegate of the shared UIApplication
object. In Swift, you can easily designate this class by adding the @UIApplicationMain
attribute to that class' declaration. (You can also still invoke the UIApplicationMain
function directly if you have reason to. In Swift you put that call in top-level code in a main.swift
file.)
@UIApplicationMain
is for iOS only. In OS X, the app delegate is traditionally set in the main nib file designated by the Info.plist (the same for Swift as for ObjC) — but with OS X storyboards there's no main nib file, so @NSApplicationMain
does the same thing there.
@UIApplicationMain attribute is a replacement of main.m file & and entry point for your application to start.
One more thing your program can work without this @UIApplicationMain all you need to do is comment //@UIApplicationMain` create main.swift same as main.m in objective c and write below code. that will be the entry point of your application
import Foundation
class FLApplication: UIApplication
{
override func sendEvent(event: UIEvent!)
{
println("Entry Point") // this is an example
}
}
The AppDelegate.swift source file has two primary functions:
It creates the entry point to your app and a run loop that delivers input events to your app. This work is done by the UIApplicationMain attribute (@UIApplicationMain), which appears toward the top of the file. UIApplicationMain creates an application object that’s responsible for managing the life cycle of the app and an app delegate object.
It defines the AppDelegate class, the blueprint for the app delegate object. The app delegate creates the window where your app’s content is drawn and provides a place to respond to state transitions within the app. The AppDelegate class is where you write your custom app-level code.
Now that the Swift documentation is updated, here is the relevant passage:
NSApplicationMain
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.
https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html