gpt4 book ai didi

SwiftUI - 如何访问列表中的数据

转载 作者:行者123 更新时间:2023-11-30 10:27:49 24 4
gpt4 key购买 nike

我想更改列表中的值。这就是我所拥有的:

struct ExpenseItem {
let value : String
let currency : String
let shopName : String
let code : String
}

final class Expenses: ObservableObject {
@Published var items = [ExpenseItem]()
}

我现在如何访问这些数据?用下面的方法是不行的。它说

Value of type '[ExpenseItem]' has no member 'value'

@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迭代所有项目:

@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)
}
}
}
}

关于SwiftUI - 如何访问列表中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59775380/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com