I am trying to loop over @AppStorage variables and use them to create TextFields with predefined values. I've tried several things as adding and removing $
for variable1-5 and variable, but nothing seems to work.
To make it easier I reduced the amount of variables and removed all the unneeded code.
import SwiftUI
struct ContentView: View {
static let store = UserDefaults(suiteName: "group.not-important.etc")
@AppStorage("variable1", store: store) var variable1 = "a"
@AppStorage("variable2", store: store) var variable2 = "b"
@AppStorage("variable3", store: store) var variable3 = "c"
@AppStorage("variable4", store: store) var variable4 = "d"
@AppStorage("variable5", store: store) var variable5 = "e"
var body: some View {
NavigationStack {
Form {
Section(header: Text("Variables")) {
HStack (alignment: .center) {
ForEach([
$variable1, // also tried variable1, etc...
$variable2,
$variable3,
$variable4,
$variable5
], id: \.self) { variable in // also tried $variable
TextField(
"",
text: variable // also tried $variable, variable.wrappedName, variable.text, etc...
)
}
}
}
}
}
}
}
Please help to understand what is the issue here and why I can not get it to work.
More info on errors here:
Provided example throws an error for
ForEach
line:Generic struct 'ForEach' requires that 'Binding<String>' conform to 'Hashable'
When trying
$variable in
instead ofvariable in
I get:Inferred projection type 'String' is not a property wrapper
TextField("", text: $variable) throws:
Cannot convert value of type 'String' to expected argument type 'Binding<String>'