Xcode 12 & SwiftUI: Cannot preview in this file — Failed to update preview

徘徊边缘 提交于 2021-02-08 13:38:38

问题


Seems like previews stopped working on Xcode 12! Trying to preview the SwiftUI file template and getting the error below. Any ideas how to fix this? Tried cleaning the build folder, deleting derived data and restarting Xcode to no avail!

On Catalina 10.15.6.

RemoteHumanReadableError: Failed to update preview.

The preview process appears to have crashed.

Error encountered when sending 'prepare' message to agent.

==================================

|  RemoteHumanReadableError: The operation couldn’t be completed. (BSServiceConnectionErrorDomain error 3.)
|  
|  BSServiceConnectionErrorDomain (3):
|  ==BSErrorCodeDescription: OperationFailed

The code I am trying to preview (from SwiftUI new file template):

import SwiftUI

struct SwiftUIView: View {
    var body: some View {
        Text("Hello, World!")
    }
}

struct SwiftUIView_Previews: PreviewProvider {
    static var previews: some View {
        SwiftUIView()
    }
}

回答1:


Apparently it was a bug with Firestore, which is most likely the cause: XCode 12 Preview Crashes when adding Firebase SDK Swift UI 2.0. The best solution at this point is to call, pod update which should solve the issue. I verified on Xcode 12, iOS 13+.




回答2:


If your SwiftUIView use ObservableObject as environmentObject, try this:

struct SwiftUIView_Previews: PreviewProvider {
    static var previews: some View {
        SwiftUIView().environmentObject(YourObservableObjectClass())
    }
}



回答3:


I had the same issue when I updated Firebase Analytics (to v6.33.0). Commenting out the following line fixed it.

FirebaseApp.configure()


来源:https://stackoverflow.com/questions/64038620/xcode-12-swiftui-cannot-preview-in-this-file-failed-to-update-preview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!