未命名参数 #2 必须在参数 'destination' 之前

Unnamed argument #2 must precede argument 'destination'

我正在尝试制作一个餐馆位置列表,每个位置都显示在 EateryRow 中,可以单击它以移动到 EateryDetail 页面,但是随着此代码的实施,我收到了一个错误,我相信与 NavigationLink 参数的语法有关。

另外:我发现这个 question 似乎和我有同样的问题,但仍然没有答案。

import SwiftUI

struct EateryList: View {
    @Binding var eateries: [Eatery]
    var body: some View {
        NavigationView {
            VStack {
                List {
                    ForEach(eateries) {
                        NavigationLink(destination: EateryDetail(eatery: $eateries[identifiedBy: [=11=]])) { //error here
                               EateryRow(eatery: $eateries[identifiedBy: [=11=]])
                            }
                        }
                    .onMove {
                        eateries.move(fromOffsets: [=11=], toOffset: )
                        EateriesApp.save()
                    }.onDelete {
                        eateries.remove(atOffsets: [=11=])
                        EateriesApp.save()
                    }
                }
                
                .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"))
        EateriesApp.save()
    }
}

我在 NavigationLink 的行上收到此错误:

Unnamed argument #2 must precede argument 'destination'

为了进一步清楚,这是我在 EateryDetail 和 EatertyRow 视图中使用“eatery”变量的方式:

struct EateryDetail: View {
    @Binding var eatery: Eatery
struct EateryRow: View {
    @Binding var eatery: Eatery

这是我在名为 eateries.swift:

的文件中定义的 Eatery 代码
import Foundation

struct Eatery: Codable, Identifiable {
    var id = UUID()
    var name: String
    var location: String
    var notes: String
    var reviews: [String] = []
    var url: String = ""
}

在eateriesApp.swift中也是这样定义的:

import SwiftUI

@main
struct EateriesApp: App {
    @State var model: [Eatery] = EateriesApp.model
    static var model: [Eatery] = {
        guard let data = try? Data(contentsOf: EateriesApp.fileURL),
              let model = try? JSONDecoder().decode([Eatery].self, from: data) else {
        return [elCaminoCantina, theFineDine, nightBites, theRiverRodeo, theCozyKitchen, theElegantEatery]
        }
        return model
    }()
    static var modelBinding: Binding<[Eatery]>?

    var body: some Scene {
        EateriesApp.modelBinding = $model
        return WindowGroup {
            ContentView(eateries: $model)
        }
    }

您应该需要在 ForEach(eateries) 中使用 .indices

像这样

ForEach(eateries.indices) { index in
    NavigationLink(destination: EateryDetail(eatery: $eateries[index])) { //error here
        EateryRow(eatery: $eateries[index])
    }
}

问题是您正在使用 shorthand 变量 ($0)。当您在 NavigationLink 中使用 $0 时,$0 将被视为 NavigationLink 而不是 ForEach。所以现在 $0 在你的情况下有冲突。

您可以使用以下代码进行检查。在下面的代码中现在不会产生任何错误,因为现在 NavigationLink

中没有使用 $0
ForEach(eateries) {
    Text([=11=].name)
    NavigationLink(destination: EateryDetail(eatery: $eateries[identifiedBy: [=11=]])) {
        Text("[=11=].name")
    }
}

另一种解决方案是使用一个变量并像这样存储您的 $0 数据。

ForEach(eateries) {
    let eaterie = [=12=] //<--- Here
    NavigationLink(destination: EateryDetail(eatery: $eateries[identifiedBy: eaterie])) { //<--- Here
        EateryRow(eatery: $eateries[identifiedBy: eaterie]) //<--- Here
    }
}