sul*_*n.h 5 xcode swift swiftui
我创建了一个新的 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()
}
}
Run Code Online (Sandbox Code Playgroud)
我遇到了同样的问题,我可以在模拟器/真实设备中构建项目,没有任何问题,但我无法利用 SwiftUI 预览功能。
\n我有相同的错误消息“消息发送失败”,其中诊断报告提到他们无法找到相关的 SwiftUI 视图文件。我已向 Apple 提出审核请求。
\n与此同时,我通过关闭“自动刷新画布”选项找到了解决方法
\nXcode >> 编辑器 >> 画布 >> 自动刷新画布。
\n这允许预览,至少加载,并且您可以使用设备中的播放按钮手动刷新它们。
\n\n关闭后,它将加载预览,您可以单击播放按钮刷新预览。
\n--2022年1月12日更新--
\n在收到 AppleDTS 认为这是一个错误的电子邮件后,我们花了很多时间来解决这个问题,并找到了一个可重复的解决方法。进一步的请求需要通过反馈助手提出,如果我发现任何有用的东西,我会分享苹果的反馈。
\n就我而言,这个问题时不时地再次出现,并且如果您使用M1 Macbook Pro并在构建中排除 Arm64 架构以及各种 cocoapods ,则找到了一种(相当肮脏的)利用 SwiftUI 的方法。以下是迄今为止我发现的最安全/万无一失的方法,至少对我来说是这样,让预览正常工作。
\n如果您同时找到了更好的解决方案,请随时分享。这篇 SO 帖子是引导我找到临时解决方法的帖子。
\n\xe2\x80\x9c无法在此文件中预览 - 连接中断:将 PreviewInstances 消息发送给代理”Xcode 12 中的错误
\n| 归档时间: |
|
| 查看次数: |
3751 次 |
| 最近记录: |