1
votes

In SwiftUI, I have a struct as shown below. There's a binding var that is a Bool. For the preview, what does Xcode expect in place of the Binding<Bool> placeholder? true and false return the error: "Cannot convert value of type 'Bool' to expected argument type '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")
    }
}
2

2 Answers

1
votes

You are getting that error because SwiftUI expects that you pass a value to DetailShellView, but you are not passing anything. For example:

I expect oranges, and you give me <<#Fruit#>>. It's a silly example, but the point is that on your class, you need to pass an actuall bool value, so Biding is a way of saying, I'm getting the value from somewhere else and I'll use it. Your class DetailShellView is expecting such value.

So what you can do is pass in an actual value like so:

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

You can preview both states, eg. as follow

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