【发布时间】:2021-03-27 16:24:34
【问题描述】:
这是一个很受欢迎的post,但它没有回答我的问题,因为我们的查询方法不同。
假设我有一个数组:
final class SomeClass: ObservableObject {
@Published var myArray = ["cat","dog","mouse"]
func changeValueFor(index: Int, to newName: String) {
//Dont know what to put here, See below for the picture of the method I thought I could use
}
}
我想访问它的属性并在我的视图中更改它:
struct MyView: View {
@ObservedObject var vm: SomeClass
index: Int
var body: some View {
Text(vm.myArray[index])
Button("Change Name To") {vm.changeValueFor(index: index, to: "goat" }
}
}
访问数组以在视图上显示的正常方法只是从一些提供的迭代器(如ForEach)传入Int,但在这种情况下,我不知道该怎么做......
我以为我可以用这个????,但显然不能
希望我足够清楚,但如果我需要更多解释我的问题,请告诉我。
【问题讨论】: