8

I need help understanding when to use @main vs @uiapplicationmain.

From what I know, @uiapplicationmain is an easier way of calling the uiapplicationmain(:::) where the class that is made after @uiapplicationmain is used as the App Delegate. I also know that it also creates an UIApplication.

I also know that app delegate becomes the entry point for the project.

From what I have read I was told that @main is also a entry point but it requires an main function.

What I want to know is what do they mean by the main entry point. Like what is Xcode doing to make it the "entry" point. And how does the whole @main thing work, as in how does it differ from @uiapplicationmain and what is it doing to the way Xcode runs the code.

ricardopereira
  • 11,118
  • 5
  • 63
  • 81
jdubk
  • 95
  • 1
  • 5

2 Answers2

14

All code has an entry point: the place where whoever calls that code actually calls. How does the whole program, comprising many Swift files, actually get started? We need an entry point for the whole program, which the runtime will call to launch us.

In Swift, this is the main.swift file. Its job is to call UIApplicationMain, which creates some instances including the app and the app delegate and gets the event loop running (and stays running for the rest of the time the app runs). A minimal main.swift file would have to look like this:

import UIKit
UIApplicationMain(
    CommandLine.argc, CommandLine.unsafeArgv, nil, 
        NSStringFromClass(AppDelegate.self)
)

However, no one ever uses a main.swift file! It's boilerplate, so why bother? Instead, you say @main, and a main.swift file is generated for you behind the scenes. In particular, you put the attribute @main on your AppDelegate class, so the main.swift generator knows which class to instantiate as your application delegate.

Two more things to know:

  • Before Swift 5.3, @main was called @UIApplicationMain instead. From that point of view, they are identical, two names for the same thing.

  • New in Swift 5.3 and Xcode 12, you can designate one of your own types as @main and give it a static main function, where you do whatever you would have done in the main.swift file. That is something @UIApplicationMain cannot do:

    @main
    struct MyMain {
        static func main() -> Void {
             UIApplicationMain(
                 CommandLine.argc, CommandLine.unsafeArgv, nil, NSStringFromClass(AppDelegate.self)
             )
        }
    }

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • sorry could you explain further the second pointer because it isn't clicking for me. From my understanding your saying that @main also allows you to specify a main function anywhere you want and works the same as main.swift file. – jdubk May 11 '21 at 06:05
  • As I demonstrate at https://github.com/mattneub/Programming-iOS-Book-Examples/blob/604368f458076dd40a8f4d0676a2aa79adb8e390/bk1ch06p297main2/bk1ch06p297main/AppDelegate.swift – matt May 11 '21 at 06:07
4

@main is part of the new SwiftUI lifecycle, introduced in iOS 14.

@uiapplicationmain is the older version that's part of UISceneDelegate and UIApplicationDelegate, but is still in use. Both represent your app's lifecycle.

aheze
  • 24,434
  • 8
  • 68
  • 125