flymg
flymg

Reputation: 617

Cast AppDelegate in SwiftUI Lifecycle

How do you do the cast below, in an App with SwiftUI Lifecycle, where AppDelegate is implemented as NSApplicationDelegateAdaptor?

let appDelegate = NSApplication.shared.delegate as! AppDelegate

// or
let appDelegate = NSApp.delegate as! AppDelegate

The above throws the following error:

Could not cast value of type 'SwiftUI.AppDelegate' (0x1e28fae40) to 'MyApp.AppDelegate' (0x102d277c0).

Background is using AppDelegate in an Extension

extension NSStatusBarButton {
    public override func mouseDown(with event: NSEvent) {
        super.mouseDown(with: event)
        let appDelegate = NSApp.delegate as! AppDelegate
        // do stuff with appDelegate on MouseDown Event
    }
}

Upvotes: 6

Views: 1770

Answers (1)

Asperi
Asperi

Reputation: 257493

We must not cast, because NSApp's delegate is not our delegate, but internal, which redirects some callbacks to that one injected via adapter.

We can access our delegate directly via variable of adapter:

@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate

or via EnvironmentObject (for that AppDelegate have confirm to ObservableObject protocol), which is injected into ContentView for us automatically and so available inside all subviews:

struct MyView: View {
  @EnvironmentObject var appDelegate: AppDelegate

  // ...

Upvotes: 8

Related Questions