Avatar ·

How to get a reference to AppDelegate in Swift: UIApplication.shared.delegate and alternatives

Hello everyone! I just started learning Swift and iOS development, and I ran into a problem. I need to access some data and methods stored in my AppDelegate from other controllers and classes. I know that AppDelegate is a singleton created by the system, but I don't understand exactly how to get a reference to it.

\n\n

Here is a simplified version of my AppDelegate:

\n\n
import UIKit\n\n@UIApplicationMain\nclass AppDelegate: UIResponder, UIApplicationDelegate {\n    \n    var window: UIWindow?\n    var userToken: String? // Authorization token needed in many controllers\n    \n    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {\n        userToken = "some_token_