Skip to content
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

Open
jaywcjlove opened this issue Apr 25, 2021 · 3 comments
Open

Cannot convert value of type 'String' to 'Binding<String>' #6

jaywcjlove opened this issue Apr 25, 2021 · 3 comments
Labels

Comments

@jaywcjlove
Copy link
Owner

jaywcjlove commented Apr 25, 2021

https://stackoverflow.com/a/59891334/1334703

TextField("placeholder", text: .constant(""))
struct SignInButtons_Previews: PreviewProvider {
    static var previews: some View {
        SignInButtons(backgroundColor: Color.blue,
                      signInUrl: .constant("http://www.apple.com"),
                      showModal: .constant(false))
    }
}
@jaywcjlove
Copy link
Owner Author

jaywcjlove commented Apr 25, 2021

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
}

@jaywcjlove
Copy link
Owner Author

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"]))
    }
}

@jaywcjlove
Copy link
Owner Author

jaywcjlove commented Apr 25, 2021

Cannot convert value of type 'String' to 'Binding<String>'

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
Labels
Projects
None yet
Development

No branches or pull requests

1 participant