Where is main file in swift
In Objective C we have main.c file where the application life cycle starts from.
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
int main(int argc, char * argv[]) {
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
In this function, UIApplicationMain from UIKit is invoked. It will initiate an object of UIApplication or its subclass and as you can see in the third argument nil is passed in this example, which means the default UIApplication will be used. The last argument specifies which class to be used as the app delegate. An instance of it will be created in UIApplicationMain and set as the delegate property of UIApplication object. This object is used to receive some life cycle methods like didFinishLaunching and didEnterBackground.
Although UIApplicationMain returns an int, it will never return and be left in the memory, until the system or user kills it.
When you create an iOS project in Swift, there is no main file in the project, neither the main function. but there is a only thing seems to be related to main that is @UIApplicationMain attribute, which is above the default Appdelegate class declaration.
If we mark a class as @UIApplicationMain, the compiler will assume that class as a Appdelegate class. Other things are more likely boilerplate code, so they are inserted in compiling time automatically.
You can try to remove the @UIApplicationMain attribute, which will cause an error when compiling and we don’t need to remove this attribute until or unless you need to create your own Appdelegate class.
Thank you for reading
I hope it will be useful for you. Please let me know your thoughts and opinions :). If you enjoyed it, feel free to hit the clap button below 👏 to help others find it!