【问题标题】:Unnamed argument #2 must precede argument 'destination'未命名的参数 #2 必须在参数“目标”之前
【发布时间】:2021-04-30 04:05:46
【问题描述】:

我正在尝试制作一个餐馆位置列表,每个位置都显示在 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: $0])) { //error here
                               EateryRow(eatery: $eateries[identifiedBy: $0])
                            }
                        }
                    .onMove {
                        eateries.move(fromOffsets: $0, toOffset: $1)
                        EateriesApp.save()
                    }.onDelete {
                        eateries.remove(atOffsets: $0)
                        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 时收到此错误:

未命名的参数 #2 必须在参数“目标”之前

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

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

这是我在 Eatery.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)
        }
    }

【问题讨论】:

  • 为什么是identifiedBy?顺便说一句,您可以发布Eatery 的代码吗?
  • 使用ForEach(eateries) { eaterie in而不是$0。

标签: swift xcode foreach swiftui swiftui-navigationlink


【解决方案1】:

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

这样

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

问题是您使用的是速记变量 ($0)。当您在 NavigationLink 中使用 $0 时,NavigationLink 会考虑 $0 而不是 ForEach。所以现在这两个 $0 在你的情况下都存在冲突。

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

ForEach(eateries) {
    Text($0.name)
    NavigationLink(destination: EateryDetail(eatery: $eateries[identifiedBy: $0])) {
        Text("$0.name")
    }
}

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

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

【讨论】:

  • 感谢您的回答,不幸的是,当我从列表中删除任何项目时,我得到“致命错误:索引超出范围”?关于如何解决这个问题的任何想法?
  • 实际上我已经实施了您提供的另一个最后的解决方案,它似乎通过一些调整解决了我所有的问题。非常感谢您的帮助
  • @kmurp62rulz index out of range on delete action with ForEach,你可以在堆栈溢出中找到这个问题。问题太多了。
猜你喜欢
  • 1970-01-01
  • 2020-01-23
  • 1970-01-01
  • 2010-11-08
  • 1970-01-01
  • 2012-01-01
  • 2020-08-06
  • 2013-10-18
  • 2018-11-05
相关资源
最近更新 更多