【问题标题】:Cannot preview in this file -- Message send failure无法在此文件中预览 - 消息发送失败
【发布时间】:2021-12-20 01:49:49
【问题描述】:

我创建了一个新的 SwiftUI 项目,但代码不会在实时预览窗口中加载我每次都收到以下错误:

无法在此文件中预览 -。但是当我在模拟器上运行它时,它也适用于其他视图。

这是代码

import SwiftUI
import FirebaseAuth


class AppViewModel: ObservableObject {
    
    let auth = Auth.auth()
    
    @Published var LoggedIn = false
    
    var isLoggedIn: Bool{
        return auth.currentUser != nil
    }
    
    func LogIn(email: String, password: String) {
        
        auth.signIn(withEmail: email, password: password) { [weak self] result, error in
            guard result != nil, error == nil else{
                return
            }
            
            DispatchQueue.main.async {
            // Success
            self?.LoggedIn = true
            }
        }
        
    }
    
    func SignUp(email: String, password: String) {
        
        auth.createUser(withEmail: email, password: password) { [weak self] result, error in
            guard result != nil, error == nil else{
                return
            }
            DispatchQueue.main.async {
            // Success
            self?.LoggedIn = true
            }
        }
        
    }
}

struct ContentView: View {
    @EnvironmentObject var ViewModel : AppViewModel
    var body: some View {
        NavigationView {
        if ViewModel.LoggedIn {
            Text ("You are Logged In")
            } else{
                LogInView()
        }
    }
        .onAppear {
            ViewModel.LoggedIn = ViewModel.isLoggedIn
        }
    }
}

struct LogInView: View {
    @State var email = ""
    @State var password = ""
    
    @EnvironmentObject var ViewModel : AppViewModel
    var body: some View {
        
        VStack {
            
            TextField("Email Adress", text: $email)
                .disableAutocorrection(true)
                .autocapitalization(.none)
                .padding()
                .background(Color(.secondarySystemBackground))
                .cornerRadius(15)
            
            SecureField("Password", text: $password)
                .disableAutocorrection(true)
                .autocapitalization(.none)
                .padding()
                .background(Color(.secondarySystemBackground))
                .cornerRadius(15)
            
            Button(action: {
                
                guard !email.isEmpty, !password.isEmpty else{
                    return
                }
                ViewModel.LogIn(email: email, password: password)
            }) {
                Text("Log In")
                    .foregroundColor(Color.white)
                    .font(.headline)
                    .frame(maxWidth: .infinity)
                    .frame(height: 50)
                    .background(Color.accentColor)
                    .cornerRadius(20.0)
            }
            
            NavigationLink("Create an account", destination: SignUpView())
                .padding()
            

            
        }
        .padding(.horizontal, 24.0)
        .navigationTitle("Welcome")
    }
}


struct SignUpView: View {
    @State var email = ""
    @State var password = ""
    
    @EnvironmentObject var ViewModel : AppViewModel
    var body: some View {
        
        VStack {
            
            TextField("Email Adress", text: $email)
                .disableAutocorrection(true)
                .autocapitalization(.none)
                .padding()
                .background(Color(.secondarySystemBackground))
                .cornerRadius(15)
            
            SecureField("Password", text: $password)
                .disableAutocorrection(true)
                .autocapitalization(.none)
                .padding()
                .background(Color(.secondarySystemBackground))
                .cornerRadius(15)
            
            Button(action: {
                
                guard !email.isEmpty, !password.isEmpty else{
                    return
                }
                ViewModel.SignUp(email: email, password: password)
            }) {
                Text("Sign Up")
                    .foregroundColor(Color.white)
                    .font(.headline)
                    .frame(maxWidth: .infinity)
                    .frame(height: 50)
                    .background(Color.accentColor)
                    .cornerRadius(20.0)
            }
            
            
        }
        .padding(.horizontal, 24.0)
        .navigationTitle("Create an Account")
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

【问题讨论】:

  • 不确定这是否是唯一原因,但您的 ContentView 中有一个 @EnvironmentObject,您没有为预览设置。顺便说一句,您是否单击了“诊断”按钮?
  • 这能回答你的问题吗? How to set an Environment Object in preview
  • 在预览中加载 Firebase Auth 如果它不起作用,我不会感到惊讶。您可以尝试使您的视图独立于库并注入身份验证状态。

标签: swift xcode swiftui


【解决方案1】:

我有同样的问题,我可以在模拟器/真实设备中毫无问题地构建项目,但我无法使用 SwiftUI 预览功能。

我收到了相同的错误消息“消息发送失败”,其中诊断报告提到他们无法找到相关的 SwiftUI 视图文件。我已向 Apple 提出审核请求。

与此同时,我通过

处关闭“自动刷新画布”选项找到了解决方法

Xcode >> 编辑器 >> 画布 >> 自动刷新画布。

这允许至少加载预览,并且您可以使用设备中的播放按钮手动刷新它们。

关闭后会加载预览,你可以点击播放按钮刷新预览。

--2022 年 1 月 12 日更新--

在收到来自 AppleDTS 的电子邮件后,他们认为这是一个错误,花了很多时间解决这个问题并找到了可重复的解决方法。需要通过反馈助手提出进一步的请求,如果我发现任何有用的信息,我们会分享来自 Apple 的反馈。

就我而言,如果您使用的是 M1 Macbook Pro不包括 Arm64 架构 在构建中,以及各种 cocoapods。以下是迄今为止我发现的最安全/最安全的方法,至少在我这边,可以让预览工作。

  1. 清理 Xcode(派生数据、设备支持、构建缓存)
  2. (如果你有 cocoapods 和 XCworkspace 文件)arch -x86_64 pod deintegrate in terminal
  3. 删除旧的 XCworkspace 文件和 Podfile.lock
  4. 重新安装 pod - arch -x86_64 pod install
  5. 在 Xcode 信息中勾选“Open With Rosetta”
  6. 打开新的 XCworkspace 文件
  7. 运行 iOS 模拟器为 iPhone 构建缓存(如果有 iOS 应用)
  8. 运行 WatchOS Simulator 为手表构建缓存(如果有 watchOS 应用)
  9. 确保此时 cocoapods/swift 版本等没有其他问题。
  10. 关闭“自动刷新画布”以检查预览是否加载,并在打开自动画布刷新功能时(在对 contentView/SwiftUIview 进行任何更改时)看到“消息发送失败”错误消息。
  11. 关闭 Xcode 文件,不删除构建缓存
  12. 取消选中 Xcode 信息中的“使用 Rosetta 打开”。
  13. 重新打开 XCworkspace 文件。
  14. (可选)运行模拟器
  15. 在打开“自动刷新画布”的同时使用 SwiftUI。

如果您在此期间找到了更好的解决方案,请随时分享。这篇 SO 帖子是让我找到临时解决方法的帖子。

“Cannot preview in this file - Connection interrupted: send previewInstances message to agent" error in Xcode 12

【讨论】:

  • 我在 M1 上,只是在本地运行 Xcode 而在 Rosetta 上 not 就可以了。
  • 很高兴它成功了!
猜你喜欢
  • 1970-01-01
  • 2022-09-28
  • 2020-07-10
  • 2021-01-20
  • 2022-06-13
  • 1970-01-01
  • 2016-07-24
  • 2016-08-11
  • 2022-11-23
相关资源
最近更新 更多