Reputation: 5
The #Preview macro is frustrating me! Test data that looks obvious to me fails to build. Here is a stripped down example:-
import SwiftUI
struct TestView: View {
@State private var intValue = 0
var body: some View {
TestMenuView(intValue: $intValue)
}
}
#Preview {
TestView()
}
struct TestMenuView: View {
@Binding var intValue: Int
var body: some View {
Text("Value passed in was \($intValue)")
}
}
#Preview {
TestMenuView(intValue: 1)
}
But the #Preview of TestMenuView gives the error:- Cannot convert value of type 'Int' to expected argument type 'Binding'
Upvotes: 0
Views: 531
Reputation: 1188
The compiler is telling you that you are trying to initialize the view with an Int
type, instead of the expected type Binding<Int>
.
You can get around this in a couple of ways.
TestMenuView(intValue: .constant(1))
Replace your #Preview { }
with this:
struct TestMenuView_Previews: PreviewProvider {
struct TestMenuViewContainer: View {
@State var intValue = 1
var body: some View {
TestMenuView(intValue: $intValue)
}
}
static var previews: some View {
TestMenuViewContainer()
}
}
Upvotes: 0