-1

I am learning iOS development and the following code is from the book Programming iOS8 by Matt Neuberg. It is an app delegate class to create a window. I do not understand -

  1. In the second line what is @UIApplicationMain and why is it there? Is it a declaration of a global varibale?
  2. What is the purpose of the parameter didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?? I don't see it used anywhere in the function body.
import UIKIT
@UIApplicationMain
class AppDelegate : UIResponder, UIApplicationDelegate {

    var window : UIWindow?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

            self.window = UIWindow(frame:UIScreen.mainScreen().bounds)
            self.window!.backgroundColor = UIColor.whiteColor()
            self.window!.makeKeyAndVisible()
            return true
    }
}
DevAndArtist
  • 4,971
  • 1
  • 23
  • 48
  • 1
    you can read more about the [Application's Lifecycle](https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/TheAppLifeCycle/TheAppLifeCycle.html) on Apple's site, maybe. – holex Jul 16 '15 at 08:32
  • Thanks! That's a great resource! – Schrodinger's bit Jul 16 '15 at 14:28

1 Answers1

2

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.

Reference from HERE.

And for your second point didFinishLaunchingWithOptions:

Every app begins with UIApplicationDelegate -application:didFinishLaunchingWithOptions: (or more accurately, -application:willFinishLaunchingWithOptions:, when implemented). It is called by the application to notify its delegate that the launch process is finishing, and nearly ready to run.

Community
  • 1
  • 1
Dharmesh Kheni
  • 71,228
  • 33
  • 160
  • 165