title: Shared AppDelegate is nil when set via SwiftUI App
body: <p>I need to get access to a global object. I used to rely on the <code>UIApplication.shared.delegate</code> to reference any global variables I stored there. Now, the new SwiftUI way has taken that from me. In leu of a better global object I would like to still use the <code>UIApplicationDelegate</code>. I found a few tutorials on the web that said to set it up this way.</p>
  <p>I make the app delegate like this.</p>
  <pre class="lang-swift prettyprint-override"><code>class AppDelegate: NSObject, UIApplicationDelegate {
      
      func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -&gt; Bool {
          print(&quot;Your code here&quot;)
          return true
      }
  }
  </code></pre>
  <p>And I set it like this.</p>
  <pre class="lang-swift prettyprint-override"><code>@main
  struct MyApp: App {
      
      @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
      
      var body: some Scene {
          WindowGroup {
              RootView()
          }
      }
  }
  </code></pre>
  <p>However when I access the app delegate like this I hit the <code>fatalError</code></p>
  <pre class="lang-swift prettyprint-override"><code>guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
      fatalError(&quot;No App Delegate&quot;)
  }
  </code></pre>
  <p>How can I access the AppDelegate globally now? Is there an alternative for a truly static global variable in the App now?</p>