Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "@UIApplicationMain" mean?

Tags:

swift

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 { ...  
like image 792
János Avatar asked Jul 01 '14 17:07

János


People also ask

What is UIApplicationMain?

UIApplicationMain(_:_:_:_:)Creates the application object and the application delegate and sets up the event cycle.

What does @main do in Swift?

Xcode 12 ships with Swift 5.3 which introduces the @main attribute as a general purpose way of marking the entry point of a Swift program. You use it in place of Apple specific attributes like @UIApplicationMain and @NSApplicationMain . If you're writing iOS or macOS apps you can probably just accept that and move on.


2 Answers

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.

like image 92
rickster Avatar answered Oct 19 '22 20:10

rickster


@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     } } 
like image 40
Priyanka Avatar answered Oct 19 '22 20:10

Priyanka