我想更改列表中的值。那就是我所拥有的:
struct ExpenseItem {
let value : String
let currency : String
let shopName : String
let code : String
}
final class Expenses: ObservableObject {
@Published var items = [ExpenseItem]()
}
我现在如何访问这些数据?通过以下方法不起作用。它说
类型“ [ExpenseItem]”的值没有成员“值”
@EnvironmentObject var expenses: Expenses
@State private var value = ""
var body: some View {
Form() {
Section(header: Text("Wert")) {
TextField(self.expenses.items.value, text: $value)
最佳答案
使用ForEach
遍历所有items
:
@EnvironmentObject var expenses: Expenses
var body: some View {
Form() {
Section(header: Text("Wert")) {
ForEach(0 ..< self.expenses.items.count) { i in
TextField(self.expenses.items[i].value, text: self.$expenses.items[i].value)
}
}
}
}