swift - 使用 SwiftUI 预览崩溃 @Binding : communication with the app was interrupted

标签 swift xcode swiftui xcode11

所以我有一个 XCode 项目,它有 2 个 .swift 文件,它们通过 SwiftUI 的 @Binding 共享一个变量。

该项目可以很好地构建并通过模拟器运行。

但是每当我尝试在辅助文件(从主文件接收变量)上使用预览时,它在成功构建后崩溃并显示“MyProject.app 崩溃:与应用程序的通信被中断”。

我仍然可以通过以下方式测试项目:

  1. 使用模拟器
  2. 预览主文件 (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

我想对辅助文件使用预览,这样我就可以始终停留在它的 View 上并节省时间。任何帮助将不胜感激。

最佳答案

替换

#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

关于swift - 使用 SwiftUI 预览崩溃 @Binding : communication with the app was interrupted,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57046541/

相关文章:

ios - 如何持久化附加到全局数组 swift 2 的数据

string - swift NSLocalizedString 字符串获取值错误

ios - 从任何 URL 获取特定图像,就像 Facebook 那样

macos - 如何让 SwiftUI List/OutlineGroup 懒惰地用于像文件系统这样的大树?

swiftui - Swift ScrollView 再次置顶

swift - 从属性覆盖调用 super 时出现链接错误

ios - swift : Error: 'required' initializer 'init(coder:)' must be provided by subclass of 'UIView'

ios - 使用 XCode 7 运行单元测试目标时崩溃

ios - 无法在 iOS 11 GM 中更改多个文本字段颜色

ios - 在支持旧版 iOS 的同时使用 iOS 16 环境变量?