简体   繁体   English

通过 SwiftUI App 设置时,Shared AppDelegate 为 nil

[英]Shared AppDelegate is nil when set via SwiftUI App

I need to get access to a global object.我需要访问全局 object。 I used to rely on the UIApplication.shared.delegate to reference any global variables I stored there.我曾经依靠UIApplication.shared.delegate来引用我存储在那里的任何全局变量。 Now, the new SwiftUI way has taken that from me.现在,新的 SwiftUI 方式已经从我这里拿走了。 In leu of a better global object I would like to still use the UIApplicationDelegate .在更好的全局 object 的 leu 中,我仍想使用UIApplicationDelegate I found a few tutorials on the web that said to set it up this way.我在 web 上找到了一些教程,上面说要以这种方式进行设置。

I make the app delegate like this.我像这样制作应用程序委托。

class AppDelegate: NSObject, UIApplicationDelegate {
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
        print("Your code here")
        return true
    }
}

And I set it like this.我是这样设置的。

@main
struct MyApp: App {
    
    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
    
    var body: some Scene {
        WindowGroup {
            RootView()
        }
    }
}

However when I access the app delegate like this I hit the fatalError但是,当我像这样访问应用程序委托时,我遇到了fatalError

guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
    fatalError("No App Delegate")
}

How can I access the AppDelegate globally now?我现在如何在全球范围内访问 AppDelegate? Is there an alternative for a truly static global variable in the App now?现在应用程序中是否有真正的 static 全局变量的替代方案?

In terms of using a static global variable, you could define a singleton instance yourself:在使用 static 全局变量方面,您可以自己定义一个 singleton 实例:

class SharedObject : ObservableObject{
    static var shared = SharedObject() //static instance
    @Published var string = "Test"
}

struct ContentView : View {
    var body: some View {
        Text("Hello, world")
        Text("From shared: \(SharedObject.shared.string)")
    }
}

If you just need it within the view hierarchy, you could make use of something like .environmentObject :如果您只需要在视图层次结构中使用它,则可以使用.environmentObject之类的东西:

class SharedObject : ObservableObject{
    @Published var string = "Test"
}

struct ContentView : View {
    @StateObject var sharedObj = SharedObject()
    
    var body: some View {
        ChildView().environmentObject(SharedObject())
    }
}

struct ChildView : View {
    @EnvironmentObject private var sharedObj : SharedObject
    
    var body: some View {
        Text(sharedObj.string)
    }
}

You may be aware of this already, but it's worth pointing out that there's a lot of skepticism about using global static shared objects.您可能已经意识到这一点,但值得指出的是,对于使用全局 static 共享对象存在很多怀疑。 It makes testing harder, it may make things complicated for having multiple window support in iOS, etc. Just something to think about when deciding whether this is the right architectural choice for your app.它使测试变得更加困难,它可能会使事情变得复杂,因为在 iOS 等中支持多个 window 等。在决定这是否是您的应用程序的正确架构选择时需要考虑的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM