I would like to binding value from a function. But I have the following error :
Cannot convert value of type 'Int' to expected argument type 'Binding<Int>'
Example code :
struct MyFirstView: View {
var body: some View {
MySecondView(index: getIndex())
}
func getIndex() -> Int {
/* Code to get the index.... */
return index
}
}
struct MySecondView: View {
@Binding var index: Int
var body: some View {
Text("Current index : ", index)
}
}
Thanks.
You just need to generate binding on-the-fly in function (if you really need binding there), like
func getIndex() -> Binding<Int> {
/* Code to get the index.... */
return Binding(get: {index}, set: {index = $0})
// return Binding(get: {index}, set: {_ in }) // alternate
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With