2

For an abstract input field that should be rendered in ForEach, given a struct,

struct InputFieldCustom: Identifiable, Hashable {

    let id = UUID()
    let placeholder: String
    let imageResourceName: String
    var storage: String = "email"
}

I would like to set text binding from storage var of this struct, is that possible by referencing a label?

ForEach(inputFields, id: \.self) { inputStruct in
    TextField(inputStruct.placeholder, text: \.$storage)
}

(on the top of the view I have)

@State private var email = "") ?

1 Answer 1

3

Here is possible variant

ForEach(Array(inputFields.enumerated()), id: \.1) { index, inputStruct in
    TextField(inputStruct.placeholder, text: self.$inputFields[index].storage)
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.