我想绑定函数中的值。但我有以下错误:
Cannot convert value of type 'Int' to expected argument type 'Binding<Int>'
示例代码:
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)
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢。
Asp*_*eri 11
您只需要在函数中动态生成绑定(如果您确实需要在那里绑定),例如
func getIndex() -> Binding<Int> {
/* Code to get the index.... */
return Binding(get: {index}, set: {index = $0})
// return Binding(get: {index}, set: {_ in }) // alternate
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2671 次 |
| 最近记录: |