使用 SwiftUI @Binding 预览崩溃:与应用程序的通信中断
Preview crash with SwiftUI @Binding: communication with the app was interrupted
所以我有一个 XCode 项目,它有 2 个 .swift 文件,它们通过 SwiftUI 的 @Binding 共享一个变量。
通过模拟器构建项目并运行就好了。
但是每当我尝试在辅助文件(从主文件接收变量)上使用预览时,它会在成功构建后崩溃并显示 "MyProject.app crashed: communication with the app was interrupted"。
我仍然可以通过以下方式测试该项目:
- 使用模拟器
- 正在预览主文件 (ContentView.swift)(是的,预览主文件效果很好)
但是构建和测试确实很耗时,因为它需要在我的应用程序中执行多项操作才能查看辅助文件,并且每次 Xcode 刷新应用程序都会重新启动。
这是我的子文件代码:
import SwiftUI
struct Menu_Screen : View {
@Binding var TapToBegin:Bool
var body: some View {
Button(action: {
}) {
Text("A Button").color(.white).frame(width: TapToBegin ? 50:0, height: TapToBegin ? 100:0).background(Color.blue).cornerRadius(10)
}
} }
#if DEBUG
struct Menu_Screen_Previews : PreviewProvider {
@State static var BoolVariable = true
static var previews: some View {
Menu_Screen(TapToBegin: $BoolVariable)
} }
#endif
我想对辅助文件使用预览,这样我就可以始终停留在其视图上并节省时间。任何帮助将不胜感激。
#if DEBUG
struct Menu_Screen_Previews : PreviewProvider {
static var BoolVariable = true
static let BoolVariableBinding = Binding(getValue: { BoolVariable },
setValue: { BoolVariable = [=10=] })
static var previews: some View {
Menu_Screen(TapToBegin: BoolVariableBinding)
}
}
#endif
替换
#if DEBUG
struct Menu_Screen_Previews : PreviewProvider {
@State static var BoolVariable = true
static var previews: some View {
Menu_Screen(TapToBegin: $BoolVariable)
} }
#endif
和
#if DEBUG
struct Menu_Screen_Previews : PreviewProvider {
static var previews: some View {
Menu_Screen(TapToBegin: .constant(true))
} }
#endif
其他答案引导我朝着正确的方向前进,但并没有为我解决问题。我在 Xcode 中遇到了同样的错误,但我需要发送我的视图的绑定不仅仅是一个布尔值,而是一个结构数组。我发布了我的工作代码,希望它能帮助其他人解决这个错误。
struct StudentsView_Previews: PreviewProvider {
static let prevStudents: [Student] = [
Student(id: UUID().uuidString, firstName: "John", lastName: "Doe"),
Student(id: UUID().uuidString, firstName: "Jane", lastName: "Doe"),
Student(id: UUID().uuidString, firstName: "Sam", lastName: "Smith")
]
static let prevStudentsBinding = Binding.constant(prevStudents)
static let prevClassTitle = "Math 100"
static var previews: some View {
StudentsView(students: prevStudentsBinding, classTitle: prevClassTitle)
}
}
struct Students: View {
@Binding var students: [Student]
let classTitle: String
var body: some View {
List(students.indices) { idx in
Text(self.students[idx].fullName())
}
}.navigationBarTitle(classTitle)
}
struct Student : Identifiable {
var id: String
var firstName: String
var lastName: String
func fullName() -> String {
return firstName + " " + lastName
}
}
所以我有一个 XCode 项目,它有 2 个 .swift 文件,它们通过 SwiftUI 的 @Binding 共享一个变量。
通过模拟器构建项目并运行就好了。
但是每当我尝试在辅助文件(从主文件接收变量)上使用预览时,它会在成功构建后崩溃并显示 "MyProject.app crashed: communication with the app was interrupted"。
我仍然可以通过以下方式测试该项目:
- 使用模拟器
- 正在预览主文件 (ContentView.swift)(是的,预览主文件效果很好)
但是构建和测试确实很耗时,因为它需要在我的应用程序中执行多项操作才能查看辅助文件,并且每次 Xcode 刷新应用程序都会重新启动。
这是我的子文件代码:
import SwiftUI
struct Menu_Screen : View {
@Binding var TapToBegin:Bool
var body: some View {
Button(action: {
}) {
Text("A Button").color(.white).frame(width: TapToBegin ? 50:0, height: TapToBegin ? 100:0).background(Color.blue).cornerRadius(10)
}
} }
#if DEBUG
struct Menu_Screen_Previews : PreviewProvider {
@State static var BoolVariable = true
static var previews: some View {
Menu_Screen(TapToBegin: $BoolVariable)
} }
#endif
我想对辅助文件使用预览,这样我就可以始终停留在其视图上并节省时间。任何帮助将不胜感激。
#if DEBUG
struct Menu_Screen_Previews : PreviewProvider {
static var BoolVariable = true
static let BoolVariableBinding = Binding(getValue: { BoolVariable },
setValue: { BoolVariable = [=10=] })
static var previews: some View {
Menu_Screen(TapToBegin: BoolVariableBinding)
}
}
#endif
替换
#if DEBUG
struct Menu_Screen_Previews : PreviewProvider {
@State static var BoolVariable = true
static var previews: some View {
Menu_Screen(TapToBegin: $BoolVariable)
} }
#endif
和
#if DEBUG
struct Menu_Screen_Previews : PreviewProvider {
static var previews: some View {
Menu_Screen(TapToBegin: .constant(true))
} }
#endif
其他答案引导我朝着正确的方向前进,但并没有为我解决问题。我在 Xcode 中遇到了同样的错误,但我需要发送我的视图的绑定不仅仅是一个布尔值,而是一个结构数组。我发布了我的工作代码,希望它能帮助其他人解决这个错误。
struct StudentsView_Previews: PreviewProvider {
static let prevStudents: [Student] = [
Student(id: UUID().uuidString, firstName: "John", lastName: "Doe"),
Student(id: UUID().uuidString, firstName: "Jane", lastName: "Doe"),
Student(id: UUID().uuidString, firstName: "Sam", lastName: "Smith")
]
static let prevStudentsBinding = Binding.constant(prevStudents)
static let prevClassTitle = "Math 100"
static var previews: some View {
StudentsView(students: prevStudentsBinding, classTitle: prevClassTitle)
}
}
struct Students: View {
@Binding var students: [Student]
let classTitle: String
var body: some View {
List(students.indices) { idx in
Text(self.students[idx].fullName())
}
}.navigationBarTitle(classTitle)
}
struct Student : Identifiable {
var id: String
var firstName: String
var lastName: String
func fullName() -> String {
return firstName + " " + lastName
}
}