UIApplication.shared.delegate as? AppDelegate is nil

When I click button to get AppDelegate, it return nil.
How can I get AppDelegate in click action ?

...

@main
struct GymantApp: App {
   @UIApplicationDelegateAdaptor(AppDelegate.self) public var appDelegate
   ...
}

... Button

Button("test button") {
        if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
          ...
        } else {
          os_log(.debug, "appDelegate is nil")
        }
      }

... Po UIApplication.shared.delegate

po UIApplication.shared.delegate
▿ Optional<UIApplicationDelegate>
 ▿ some : <SwiftUI.AppDelegate: 0x282969760>
Please show more code, the full class.

How did you define AppDelegate class ?
Should be like this, with all the AppDelegate func, such as didFinishLaunchingWithOptions :
Code Block
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        return true
    }

I tested, I get non nil delegate.
UIApplication.shared.delegate as? AppDelegate is nil
 
 
Q