SoFunction
Updated on 2025-03-02

Detailed explanation of @UIApplicationMain example in swift

Preface

Recently, I am learning swift and encountered some knowledge points that need to be sorted out and recorded during my study. The following article mainly introduces the relevant content about @UIApplicationMain in swift. I will share it for your reference and study. I won’t say much below, let’s take a look at the detailed introduction together.

How the program starts

In the C series language, the entrances of the program are all main functions. When an Objective-C iOS app project is created, Xcode will create a file for us.

#import <UIKit/>
#import ""
int main(int argc, char * argv[])
{
 @autoreleasepool {
  return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
 }
}

We call the UIApplicationMain method of UIKit, which initializes a UIApplication or its subclass object to start receiving events based on the third parameter. When passing nil, the default UIApplication is used. The last parameter specifies the AppDelegate class as the application's delegate and is used to receive delegate methods related to the application's life cycle.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
}
- (void)applicationWillResignActive:(UIApplication *)application {
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
}
- (void)applicationWillTerminate:(UIApplication *)application {
}

Although this method indicates that an int is to be returned, it does not actually return, but remains in memory until the user or system forces the application to terminate.

Corresponding situations in swift

After creating a swift project, we found that none of the files are similar to those in Objective-C, nor do we have the main function. The only thing that has something to do with main is that there is a @UIApplicationMain tag in AppDelegate.

import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
}

The function of this tag is to use the annotated class as a delegate, create a UIApplication and start the entire program. Generally, we do not need to make any modifications to this tag, but if we want to use a subclass of UIApplication instead of it itself, we need to customize a file (remember to delete the @UIApplicationMain tag). We don't need to define the scope of this file, just write the code directly.

import UIKit
class MyApplication: UIApplication {
 override func sendEvent(_ event: UIEvent) {
  (event)
  print("Event sent:\(event)")
 }
}
UIApplicationMain(
 ,
 UnsafeMutableRawPointer()
  .bindMemory(
   to: UnsafeMutablePointer<Int8>.self,
   capacity: Int()),
 NSStringFromClass(),
 NSStringFromClass()
)

In this way, we can listen to each time we send an event (click a button or something).

Summarize

The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.