Why is "var delegate: UIApplicationDelegate?" made optional in UIApplication? Would that be a fatal mistake?

If the property delegatehas never been nil, the application will be in a state that cannot be restored.

class UIApplication

Declaration
unowned (unsafe) delegate var: UIApplicationDelegate?
Discussion
Each application must have an application delegation object to respond to messages associated with the application. For example, an application notifies its delegate when the application finishes launching and when its foreground or background status changes. Similarly, application-related messages coming from the system are often sent to the application delegate for processing. Xcode provides the initial delegate of the application for each application, and you will not need to change this delegate later.

Why is UIApplicationDelegate.delegatedefined as optional? The value nilcan not be restored?

Actually, if I were subclassed by UIApplication, then could I recover from the delegatevalue nil? Maybe why?

Here is a related question , but AppDelegate- this is what unites the entire application. I don’t think it could be nil.

+4
source share
2 answers

To find out why / how it works, you will need to know how UIApplication works internally, so any idea of ​​why or why this is not necessary is purely a hypothesis.

, , , , , , , .

0

, AppDelegate .

, , , , window . , , AppDelegate.swift (, application(application:didFinishLaunchingWithOptions), , , .

iOS UIKit main.swift:

UIApplicationMain(Process.argc, Process.unsafeArgv, 
  NSStringFromClass(UIApplication), NSStringFromClass(AppDelegate))

( Xcode 6, @UIApplicationMain.)

UIApplicationMain , , , . , . AppDelegate UIWindow push-, .., :

UIApplicationMain(Process.argc, Process.unsafeArgv, NSStringFromClass(UIApplication), nil)

, AppDelegate , main.swift :

var delegateClassName: String? = NSStringFromClass(AppDelegate)

if NSClassFromString("XCTestCase") != nil {
 // Unit tests are being run, so don't execute any code written in AppDelegate.swift
 delegateClassName = nil
}
UIApplicationMain(Process.argc, Process.unsafeArgv, NSStringFromClass(UIApplication), delegateClassName)
+4

Source: https://habr.com/ru/post/1651610/


All Articles