What does "@UIApplicationMain" mean?

Swift

Swift Problem Overview


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 {
... 

                       

Swift Solutions


Solution 1 - Swift

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.

Solution 2 - Swift

@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
    }
}

Solution 3 - Swift

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.

Solution 4 - Swift

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

Solution 5 - Swift

@UIApplicationMain is an attribute applied to the class -declared below- AppDelegate, to provide more information about this class.

In this case, the attribute @UIApplicationMain indicates that the class AppDelegate is the application delegate of your app.

Solution 6 - Swift

UIApplicationMain Apply this attribute to a class to indicate that it is the application delegate. Using this attribute is equivalent to calling the UIApplicationMain function and passing this class’s name as the name of the delegate class. (Source)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJ&#225;nosView Question on Stackoverflow
Solution 1 - SwiftricksterView Answer on Stackoverflow
Solution 2 - SwiftPriyankaView Answer on Stackoverflow
Solution 3 - SwiftTharinduView Answer on Stackoverflow
Solution 4 - SwiftWilliam EntrikenView Answer on Stackoverflow
Solution 5 - SwiftERNView Answer on Stackoverflow
Solution 6 - SwiftmichaelIView Answer on Stackoverflow