无法在@Binding 变量上使用 ForEach 循环
Unable to use ForEach loop on @Binding variable
我最近问了一个问题,但由于他们认为答案已经存在,所以它被删除了,所以我查看了这个 。我已经尝试将此处找到的解决方案实施到我的代码中,现在 运行 变成了一个稍微不同的错误。
基本上我正在尝试创建一个简单的应用程序,其中包含一个项目列表,单击这些项目可以导航到具有编辑功能的详细视图。数据是从 JSON 文件中读取的,这就是为什么数据处于绑定中以便可以更新的原因,这在项目的其他任何地方都不是问题。
import SwiftUI
struct EateryList: View {
@Binding var eateries: [Eatery]
var body: some View {
NavigationView {
VStack {
List {
ForEach(eateries.indicies, id: \.self) { i in
NavigationLink(destination: EateryDetail(eatery: $eateries[i])) { //errors appear here
EateryRow(eatery: $eateries[i])
}
}
}
.navigationTitle("Favourite Eateries")
.navigationBarItems(leading: EditButton(), trailing: Button( action: add)
{
Image(systemName: "plus")
}
)
.listStyle(InsetGroupedListStyle())
}
}
}
func add() {
eateries.append(Eatery(name: "Eatery", location: "Insert location here", notes: "Insert notes here", reviews: ["Insert reviews here"], url: "https://i.imgur.com/y3MMnba.png"))
}
}
我目前遇到此代码的 3 个错误,但我并不完全理解...
Generic struct 'ForEach' requires that 'Binding' conform to 'RandomAccessCollection'
Referencing subscript 'subscript(dynamicMember:)' requires wrapper 'Binding<[Eatery]>'
Value of type '[Eatery]' has no dynamic member 'indicies' using key path from root type '[Eatery]'
现在非常感谢所有帮助,如果您需要进一步说明项目的其他部分,请告诉我:)
这是一个拼写错误。是 indices
不是 indicies
ForEach(eateries.indices, id: \.self) { i in //<-- Here
NavigationLink(destination: EateryDetail(eatery: $eateries[i])) {
EateryRow(eatery: $eateries[i])
}
}
我最近问了一个问题,但由于他们认为答案已经存在,所以它被删除了,所以我查看了这个
基本上我正在尝试创建一个简单的应用程序,其中包含一个项目列表,单击这些项目可以导航到具有编辑功能的详细视图。数据是从 JSON 文件中读取的,这就是为什么数据处于绑定中以便可以更新的原因,这在项目的其他任何地方都不是问题。
import SwiftUI
struct EateryList: View {
@Binding var eateries: [Eatery]
var body: some View {
NavigationView {
VStack {
List {
ForEach(eateries.indicies, id: \.self) { i in
NavigationLink(destination: EateryDetail(eatery: $eateries[i])) { //errors appear here
EateryRow(eatery: $eateries[i])
}
}
}
.navigationTitle("Favourite Eateries")
.navigationBarItems(leading: EditButton(), trailing: Button( action: add)
{
Image(systemName: "plus")
}
)
.listStyle(InsetGroupedListStyle())
}
}
}
func add() {
eateries.append(Eatery(name: "Eatery", location: "Insert location here", notes: "Insert notes here", reviews: ["Insert reviews here"], url: "https://i.imgur.com/y3MMnba.png"))
}
}
我目前遇到此代码的 3 个错误,但我并不完全理解...
Generic struct 'ForEach' requires that 'Binding' conform to 'RandomAccessCollection'
Referencing subscript 'subscript(dynamicMember:)' requires wrapper 'Binding<[Eatery]>'
Value of type '[Eatery]' has no dynamic member 'indicies' using key path from root type '[Eatery]'
现在非常感谢所有帮助,如果您需要进一步说明项目的其他部分,请告诉我:)
这是一个拼写错误。是 indices
不是 indicies
ForEach(eateries.indices, id: \.self) { i in //<-- Here
NavigationLink(destination: EateryDetail(eatery: $eateries[i])) {
EateryRow(eatery: $eateries[i])
}
}