【问题标题】:Cannot convert value of type 'NavigationLink<some View, EditView>' to closure result type '_'无法将类型“NavigationLink<some View, EditView>”的值转换为关闭结果类型“_”
【发布时间】:2020-01-07 19:46:52
【问题描述】:

我收到以下错误:

无法将“NavigationLink”类型的值转换为 关闭结果类型'_'

你知道这里出了什么问题吗?

我的 ContentView 文件:

@ObservedObject var voucherData = VoucherData()

    var body: some View {
        NavigationView {
            ZStack {
                List {
                    ForEach(voucherData.voucherList) { voucher in
                        NavigationLink(destination: EditView(value:voucher.value, currency: voucher.currency, shopName: voucher.shopName)) {
                            VStack() {

在另一个文件中:

struct Voucher : Identifiable {
    let id = UUID()
    var value : String = ""
    var currency : String = ""
    var shopName : String = ""
}

final class VoucherData: ObservableObject {
    @Published var voucherList: [Voucher] = [
        .init(value: "100", currency: "USD", shopName: "FlyBurger")]
}

【问题讨论】:

  • 您发布的ContentView 代码中是否缺少某些代码?似乎缺少更多行或更多右括号
  • 是的,但其中没有与此错误有关的任何内容。只是带有一些 HStacks 和 Vstacks 的布局,还链接到“voucher.currency”等数据。但是当我将其注释掉时,我也会收到错误消息。
  • 没有看到更多的代码,很遗憾很难说可能出了什么问题。我复制了你给的代码,并在一个新项目中尽可能简单地填写,没有错误。
  • 项目中有两个其他文件具有相同的变量,由@Binding var value : String @Binding var currency : String @Binding var shopName : String连接,但在这些文件中没有错误。
  • 我的项目可以发给你吗?

标签: ios swift xcode swiftui


【解决方案1】:

我假设您的 EditView 只是缺少参数凭证:

struct EditView: View {

    let voucher: Voucher

    ...
}

现在你可以像这样传递代金券了:

NavigationLink(destination: EditView(voucher: voucher)) {

【讨论】:

    猜你喜欢
    • 2022-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-16
    • 2021-03-08
    相关资源
    最近更新 更多