91

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 {
... 
Forge
  • 6,538
  • 6
  • 44
  • 64
János
  • 32,867
  • 38
  • 193
  • 353

6 Answers6

150

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.

rickster
  • 124,678
  • 26
  • 272
  • 326
  • 1
    On page 58 in The Swift Programming Language I found the Attributes section, interesting. – János Jul 01 '14 at 17:54
  • 1
    I'd have quoted the docs in my answer, but there don't seem to be any for this attribute just yet. The [Attributes](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html) page you found is where you probably *would* find it. – rickster Jul 01 '14 at 17:58
  • 3
    A description for `UIApplicationMain` is there now. https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html – Quinn Taylor Sep 17 '14 at 21:44
  • 2
    Re your last sentence, note that OS X has the equivalent @NSApplicationMain – Bill Mar 15 '15 at 14:30
  • 1
    @UIApplicationMain is also for tvOS – Shaked Sayag Jun 03 '19 at 13:47
7

@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
    }
}
Yeldar Kurmangaliyev
  • 33,467
  • 12
  • 59
  • 101
Priyanka
  • 543
  • 4
  • 10
  • 1
    If I do that, it compiles and runs, but finished after installing the app and sendEvent is never called. So what? :) – StuFF mc Sep 11 '14 at 14:33
  • More info here: http://stackoverflow.com/questions/24020000/subclass-uiapplication-with-swift — but again I don't land in sendEvent... – StuFF mc Sep 11 '14 at 14:52
  • @StuFFmc thank for this info. sendEvent method not called for me too. – dellos Jul 04 '15 at 05:55
  • 3
    The `Foundation` import, the `FLApplication`, the random subclassing without declaring any using of the subclass... Sorry, but none of this answer makes sense except for the first sentence. – ephemer Jul 10 '18 at 14:27
7

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.

Tharindu
  • 321
  • 3
  • 5
  • Hey @Manju....... I have overridden my UIApplication main as TimerUiApplication.............I have a variable in it..........How do i modify that variable of my UIApplication from my uiViewController..................... – Cloy Apr 11 '16 at 14:23
5

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

Community
  • 1
  • 1
William Entriken
  • 37,208
  • 23
  • 149
  • 195
0

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

ERN
  • 1
  • 2
0

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)

michaelI
  • 11
  • 3