如何从类(-extension)写入 SwiftUI 环境对象

Adr*_*ano 11 ios swift swiftui

鉴于以下设置:

环境变量 UserState

class UserState: ObservableObject {
    @Published var loggedIn = Auth.auth().currentUser != nil
}
Run Code Online (Sandbox Code Playgroud)

UserState 作为变量 SceneDelegate

class SceneDelegate: UIResponder, UIWindowSceneDelegate {
   ...

   //creating the variable
   var userState = UserState()

   func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
       window.rootViewController = UIHostingController(rootView: ContentView().environmentObject(userState))
   }

   ...
}
Run Code Online (Sandbox Code Playgroud)

我现在可以通过声明在 SwiftUI 视图中完美地读/写这个变量

struct ProfileTab: View {
    @EnvironmentObject var userState: UserState
    var body: some View {
        // here I could easily read/write to userState
    }
}
Run Code Online (Sandbox Code Playgroud)

到现在为止还挺好。但是:在 SwiftUI 视图之外写入这个变量的正确方法是什么?例如,来自类或类扩展。

例子

extension AppDelegate {

    func something(loggedIn: Bool) {
        // here I would like to access the environment variable. Something like
        // userState.loggedIn = loggedIn
    }

}
Run Code Online (Sandbox Code Playgroud)

Asp*_*eri 10

这是可能的方法......

class AppDelegate: UIResponder, UIApplicationDelegate {
   //creating the variable in AppDelegate to have it shared
   var userState = UserState()
   ...
Run Code Online (Sandbox Code Playgroud)

那么,你可以...

extension AppDelegate {

    func something(loggedIn: Bool) {
        // just use it here as now it is own property
        self.userState.loggedIn = loggedIn
    }
}
Run Code Online (Sandbox Code Playgroud)

并通过共享应用程序实例在场景委托中使用它

class SceneDelegate: UIResponder, UIWindowSceneDelegate {
   ...

 func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
   // safe as it is known the one AppDelegate
   let appDelegate = UIApplication.shared.delegate as! AppDelegate
   window.rootViewController = UIHostingController(rootView: 
       ContentView().environmentObject(appDelegate.userState))
 }
Run Code Online (Sandbox Code Playgroud)