Apple's boilerplate code for UISceneDelegate contains a stub implementation for scene(_:willConnectTo:options:)
. This starts with:
guard let _ = (scene as? UIWindowScene) else { return }
Is is really possible that we end up in the else
branch here? The constructor of UIScene
mentions
Subclasses call this method to initialize the scene details.
so my guess is that UIScene
should be treated as an abstract base class that is never created directly, only from derived classes. But I didn't find any other classes derived from UIScene
. If that holds true, wouldn't it make much more sense to write a forced downcast here?
scene as! UIWindowScene
Or is there a use case where it makes sense to create a user-defined subclass? If so, what would be an example for that and how would it be used?
UIScene
, UISceneDelegate
.UIScene
: CPTemplateApplicationDashboardScene
, CPTemplateApplicationScene
, CPTemplateApplicationInstrumentClusterScene
UISceneDelegate
: CPTemplateApplicationSceneDelegate
, CPTemplateApplicationDashboardSceneDelegate
, CPTemplateApplicationInstrumentClusterSceneDelegate
Is force casting is safe?
Force casting is safe unless you share SceneDelegate with different Role
.
UIKit expects to have concrete subclass of UIScene
for each Role
.
For example, windowExternalDisplayNonInteractive
, windowApplication
, windowExternalDisplay
should connect to UIWindowScene
.
Is there a use case where it makes sense to create a user-defined subclass?
Rarely there could be, but using different UISceneDelegate
for different Role
is more preferred way.