0
class Form {
    var id = UUID()
    var name: String = ""
    var fields: [FormField] = []
}
class FormField {
    var id = UUID()
    var name: String = ""
}
struct LogCustomizationView: View {

    @State var form: Form

    var body: some View {
        VStack {
            ForEach(form.fields.indices) { idx in
                TextField("Name", text: self.$form.fields[dynamicMember: idx])
            }
        }
    }
}

The problematic line is:

TextField("Name", text: self.$form.fields[dynamicMember: idx])

The first error I got was

Missing argument label 'dynamicMember:' in subscript

So I added dynamicMember:, which is how it's shown above. Now the error I get is: Cannot convert value of type 'Int' to expected argument type 'WritableKeyPath<_, _>'

1 Answer 1

1

SwiftUI errors can be really nebulous.

You are effectively trying to bind a FormField object to a Binding<String> where neither the object adopts BindableObject nor the name ivar (which you assumably want to bind) has a publisher attached to it (@Published).

I guess you want to bind the name ivar to the TextField:

  ForEach(form.fields.indices) { idx in
    TextField("Name", text: self.$form.fields[idx].name)
  }
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I'm just starting with SwiftUI had no idea about publishers- your helpful explanation is appreciated.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.