Macos 如何确认AppDelegate的ObserveObject?

Macos 如何确认AppDelegate的ObserveObject?,macos,swiftui,combine,Macos,Swiftui,Combine,我试图观察macOS AppDelegate中的值,但出现错误 ContentView.swift:14:6:泛型结构“ObservedObject”要求“NapplicationDelegate”符合“ObservedObject” 当我尝试使用as将对象强制转换为ObservedObject时!ObservedObject我有另一个错误 ContentView.swift:14:6:泛型结构“ObservedObject”要求“ObservedObject”符合“ObservedObject

我试图观察macOS AppDelegate中的值,但出现错误

ContentView.swift:14:6:泛型结构“ObservedObject”要求“NapplicationDelegate”符合“ObservedObject”

当我尝试使用
as将对象强制转换为
ObservedObject
时!ObservedObject
我有另一个错误

ContentView.swift:14:6:泛型结构“ObservedObject”要求“ObservedObject”符合“ObservedObject”

内部
AppDelegate.swift
文件

import Cocoa
import SwiftUI
import Combine

@NSApplicationMain
class AppDelegate: NSObject, ObservableObject, NSApplicationDelegate  {
    var isFocused = true
    
    // Other code app life-cycle functions
}
import SwiftUI
import Combine

struct ContentView: View {
    @ObservedObject var appDelegate = NSApplication.shared.delegate
    
    // Other UI code
}
ContentView.swift文件中

import Cocoa
import SwiftUI
import Combine

@NSApplicationMain
class AppDelegate: NSObject, ObservableObject, NSApplicationDelegate  {
    var isFocused = true
    
    // Other code app life-cycle functions
}
import SwiftUI
import Combine

struct ContentView: View {
    @ObservedObject var appDelegate = NSApplication.shared.delegate
    
    // Other UI code
}

这看起来像是概念的混合。。我建议避免这样的。。。而是创建了显式的可观察类

如下图所示(发痒)

在ContentView中,只需使用它

struct ContentView: View {
    @EnvironmentObject var appState: AppState
    
    // Other UI code

    var body: some View {
       // .. use self.appState.isFocused where needed
    }
}