This works
import SwiftUI
struct ContentView : View {
@State var val1: Int = 0
var body: some View {
MySubview(val1: $val1)
}
}
#if DEBUG
struct ContentView_Previews : PreviewProvider {
static var previews: some View {
ContentView(val1: 0)
}
}
#endif
struct MySubview : View {
@Binding var val1: Int
var body: some View {
return Text("Value = \(val1)")
}
}
But when the variable is nested in an Object, this fails
import SwiftUI
struct MyStruct {
let number: Int
}
struct ContentView : View {
@State var val1 = MyStruct(number: 7)
var body: some View {
MySubview(val1: $val1.number)
}
}
#if DEBUG
struct ContentView_Previews : PreviewProvider {
static var previews: some View {
ContentView(val1: 0)
}
}
#endif
struct MySubview : View {
@Binding var val1: Int
var body: some View {
return Text("Value = \(val1)")
}
}
Error shown: Generic parameter 'Subject' could not be inferred
How do i pass nested variable as a binding to a subview?