According to the iOS Developer Library
The app delegate is where you write your custom app-level code. Like all classes, the AppDelegate class is defined in two source code files in your app: in the interface file, AppDelegate.h, and in the implementation file, AppDelegate.m.
However, in Xcode 6.3 it appears that there is only AppDelegate.swift and no longer the .h and .m extensions. I would like to understand how the .swift replaced both the .h and .m extensions.
h is called the Header file and the . m is called the Implementation file. Together, they form the AppDelegate class.
The app delegate is effectively the root object of your app, and it works in conjunction with UIApplication to manage some interactions with the system. Like the UIApplication object, UIKit creates your app delegate object early in your app's launch cycle so it's always present.
AppDelegate is responsible for handling application-level events, like app launch and the SceneDelegate is responsible for scene lifecycle events like scene creation, destruction and state restoration of a UISceneSession.
In all the apps built prior iOS 13, AppDelegate is the main entry of the app and it is the place where many logics and app states will be handled. It is the place where application launch and apps foreground and background logics are handled.
The simple answer is that AppDelegate.swift is just the translation from Objective-C AppDelegate.h
and AppDelegate.m
, as Swift does not require separate headers and implementations, but rather a single .swift file.
However, under the hood, there are other key differences between the two.
In Objective-C, there exists a main.m
file that's sole purpose is instantiating UIApplicationMain
In Swift, the @UIApplicationMain
annotation tag found at the top of AppDelegate.swift replaces the need for any main function that existed in the main.m file in Objective-C. If this tag is omitted, you can use a main.swift file to instantiate your UIApplication
using the specified App Delegate.
A main.swift implementation looks like this:
import UIKit
autoreleasepool {
UIApplicationMain(Process.argc, Process.unsafeArgv, nil, NSStringFromClass(AppDelegate.self))
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With