Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does AppDelegate.swift replace AppDelegate.h and AppDelegate.m in Xcode 6.3

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.

like image 787
Mark B Avatar asked May 22 '15 03:05

Mark B


People also ask

What is AppDelegate M and AppDelegate H?

h is called the Header file and the . m is called the Implementation file. Together, they form the AppDelegate class.

What is use of AppDelegate in Swift?

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.

What is the difference between AppDelegate and SceneDelegate in Swift?

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.

What is AppDelegate in Xcode?

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.


1 Answers

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))
}
like image 118
Ian Avatar answered Sep 18 '22 13:09

Ian