【问题标题】:What does Xcode expect for the "Binding<Bool>" placeholder?Xcode 对“Binding<Bool>”占位符有什么期望?
【发布时间】:2020-10-08 21:29:28
【问题描述】:

在 SwiftUI 中,我有一个如下所示的结构。有一个绑定变量是一个布尔值。对于预览版,Xcode 期望用什么来代替 Binding&lt;Bool&gt; 占位符? truefalse 返回错误:“无法将 'Bool' 类型的值转换为预期的参数类型 'Binding'”。

import SwiftUI

struct DetailShellView : View {
    @Binding var isPresented: Bool

    var testMessage: String

    var body: some View {
        VStack {
            Button(action: {
                self.isPresented = false
                print("variable message: \(self.testMessage)")
            }) {
                Text("Close modal view")
            }
            Text(testMessage)
        }
    }
}

struct DetailShellView_Previews: PreviewProvider {
    static var previews: some View {
        DetailShellView(isPresented: <#Binding<Bool>#>, testMessage: "donuts")
    }
}

【问题讨论】:

    标签: swift xcode swiftui


    【解决方案1】:

    您收到该错误是因为 SwiftUI 期望您将值传递给 DetailShellView,但您没有传递任何内容。例如:

    我期待橙子,你给我>。这是一个愚蠢的例子,但重点是在你的类上,你需要传递一个实际的 bool 值,所以 Biding 是一种说法,我从其他地方获取值,我会使用它。你的班级DetailShellView 期待这样的价值。

    所以你可以做的是像这样传递一个实际值:

    struct DetailShellView_Previews: PreviewProvider {
        static var previews: some View {
            DetailShellView(isPresented: .constant(true), testMessage: "donuts")
        }
    }
    

    【讨论】:

      【解决方案2】:

      您可以预览这两种状态,例如。如下

      static var previews: some View {
        Group {
          DetailShellView(isPresented: .constant(true), testMessage: "donuts")
          DetailShellView(isPresented: .constant(false), testMessage: "donuts")
        }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-09-16
        • 1970-01-01
        • 1970-01-01
        • 2013-12-09
        • 1970-01-01
        • 2016-04-19
        • 2018-03-14
        相关资源
        最近更新 更多