无法在此文件中预览-[App Name] .app可能在Xcode 11 Beta 5上崩溃了 [英] Cannot preview in this file - [App Name].app may have crashed on Xcode 11 Beta 5
问题描述
如果我添加EnviromentObject
属性包装器,则Xcode预览不起作用.每次我添加一个画布时,画布都不会建立,并且出现此错误:
The Xcode preview does not work if i add a EnviromentObject
property wrapper. Everytime i add one the Canvas doesn't build and i get this error:
无法在此文件中预览-[App Name] .app可能已崩溃
Cannot preview in this file - [App Name].app may have crashed
如果我将EnviromentObject
属性包装器替换为ObservedObject
并将其初始化,则一切正常.
If i replace the EnviromentObject
property wrapper with ObservedObject
and initialize it everything works fine.
这是我的代码:
class NetworkManager: ObservableObject {
}
struct ContentView : View {
@EnvironmentObject var networkManager: NetworkManager
var body: some View {
Text("Canvas not working")
}
}
#if DEBUG
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView().environmentObject(NetworkManager())
}
}
#endif
更新:
当我使用绑定时,它也不会加载预览:
It doesn’t load the preview as well when i am using a binding:
struct ContentView : View {
@EnvironmentObject var networkManager: NetworkManager
@Binding var test123: String
var body: some View {
Text("Canvas not working")
}
}
#if DEBUG
struct ContentView_Previews: PreviewProvider {
@State static var test1 = ""
static var previews: some View {
ContentView(test123: $test1).environmentObject(NetworkManager())
}
}
#endif
推荐答案
我假设基于您提供的代码,您的SceneDelegate
看起来像这样:
I'm assuming based on the code you provided that your SceneDelegate
looks like this:
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIHostingController(rootView: ContentView())
self.window = window
window.makeKeyAndVisible()
}
我不会假装我确切知道画布在生成预览时在幕后所做的事情,但是基于错误特别指出该应用程序可能崩溃的事实,我假设它是在尝试生成预览时尝试启动整个应用程序.也许它需要使用SceneDelegate
来启动预览,也许它完全是另外一回事-我不能肯定地说.
I'm not going to pretend I know exactly what the canvas is doing behind the scenes when it generates a preview, but based on the fact that the error specifically states that the app may have crashed, I'm assuming that it's attempting to launch the entire app when it tries to generate a preview. Maybe it needs to use the SceneDelegate
to launch the preview, maybe it's something else entirely - I can't say for sure.
无论如何,应用程序崩溃的原因是因为您没有在SceneDelegate
中传递环境对象.您的SceneDelegate
应该如下所示:
Regardless, the reason the app is crashing is because you aren't passing an environment object in your SceneDelegate
. Your SceneDelegate
should look like this:
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIHostingController(rootView: ContentView().environmentObject(NetworkManager()))
self.window = window
window.makeKeyAndVisible()
}
这篇关于无法在此文件中预览-[App Name] .app可能在Xcode 11 Beta 5上崩溃了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!