I get a compiler error trying to specify Binding on property of @ObservedObject using KeyPath for a generic type T.
I am trying to create a generic SwiftUI View that will work for a class that has several properties of the same type. The SwiftUI View needs to be generic; however I cannot determine how to create a Binding to a property of the main object that I access through a KeyPath. The code below returns an error of "Value of type 'T' has no subscripts"
UPDATE: I identified the error in my code instead of $model[keyPath: kp] I should have used $model[dynamicMember: kp].
import SwiftUI
class TestClass: ObservableObject {
@Published public var str1: String = "one"
@Published public var str2: String = "two"
}
struct ContentView: View {
@ObservedObject var test = TestClass()
var body: some View {
VStack {
HStack{
Text("String One").padding(20)
StringView(model: test, kp: \TestClass.str1).padding(20)
}
HStack{
Text("String Two").padding(20)
StringView(model: test, kp: \TestClass.str2).padding(20)
}
}
}
}
struct StringView<T>: View where T: ObservableObject {
@ObservedObject var model: T
var kp: ReferenceWritableKeyPath<T,String>
init(model: T, kp: ReferenceWritableKeyPath<T,String>) {
self.model = model
self.kp = kp
}
var body: some View {
HStack{
TextField("String", text: $model[keyPath: kp]) <--- COMPILER ERROR HERE
Text(model[keyPath: kp])
}
}
}
Is it possible to do this?