-
-
Notifications
You must be signed in to change notification settings - Fork 38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot convert value of type 'String' to 'Binding<String>' #6
Labels
Comments
https://stackoverflow.com/a/57340694/1334703 struct ContentView: View {
@State private var boolArr = [false, false, true, true, false]
var body: some View {
List {
ForEach(boolArr.indices) { idx in
Toggle(isOn: self.$boolArr[idx]) {
Text("boolVar = \(self.boolArr[idx] ? "ON":"OFF")")
.onAppear {
print(idx)
}
}
}
}
}
} struct ContentView: View {
@State private var boolArr = [
BoolSelect(isSelected: true),
BoolSelect(isSelected: false),
BoolSelect(isSelected: true)
]
var body: some View {
List {
ForEach(boolArr.indices) { index in
Toggle(isOn: self.$boolArr[index].isSelected) {
Text(self.boolArr[index].isSelected ? "ON":"OFF")
}
}
}
}
}
struct BoolSelect: Identifiable {
var id = UUID()
var isSelected: Bool
} |
extension Binding where Value: MutableCollection, Value.Index == Int {
func element(_ idx: Int) -> Binding<Value.Element> {
return Binding<Value.Element>(
get: {
return self.wrappedValue[idx]
}, set: { (value: Value.Element) -> () in
self.wrappedValue[idx] = value
})
}
}
struct MainView: View {
@Binding var text: [String]
var body: some View {
TextField("", text: $text.element(0))
TextField("", text: $text.element(1))
TextField("", text: $text.element(2))
}
}
struct ContentView: View {
var body: some View {
MainView(text: .constant(["2", "3", "4"]))
}
} |
struct ContentView: View {
@State private var boolArr = [
BoolSelect(isSelected: true, title: ""),
BoolSelect(isSelected: false, title: ""),
BoolSelect(isSelected: true, title: "")
]
var body: some View {
VStack(alignment: .leading) {
ForEach(boolArr.indices) { index in
Toggle(isOn: self.$boolArr[index].isSelected) {
Text(self.boolArr[index].isSelected ? "ON":"OFF")
}
TextField("placeholder", text: Binding(
get: { return self.boolArr[index].title},
set: { (newValue) in return self.boolArr[index].title = newValue}
))
}
}
}
}
struct BoolSelect: Identifiable {
var id = UUID()
var isSelected: Bool
var title: String
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://stackoverflow.com/a/59891334/1334703
The text was updated successfully, but these errors were encountered: