SwiftUI-列出嵌套数组中的对象

时间:2019-10-22 19:42:14

标签: arrays swiftui swiftui-list

我正在尝试在嵌套数组中显示对象值。下面是我要在列表元素中显示的数据模型和详细信息页面。

// Passports.swift //

import Foundation
import SwiftUI

struct Passports: Identifiable {
    let id : Int
    let passportPremium: Bool
    let passportActive: Bool
    let passportTitle: String
    let passportDates: String
    let venues: [Venue]
    }

struct Venue: Identifiable {

    let id = UUID()
    let title : String
    let venueArea: String
    let venueItems: [venueItem]
}

struct venueItem {
    let title: String
    let productDescription: String
    let productPrice: Double
    let productType: String
    let newStatus: Bool
    let diningPlan: Bool
    let kidFriendly: Bool
    let vegetarian: Bool
    let glutenFree: Bool
    let featuredProduct: Bool
    let containsAlcohol: Bool
}


extension Passports {
    static func all() -> [Passports] {
        return [
            Passports (
                id: 1001,
                passportPremium: false,
                passportActive: true,
                passportTitle : "Passport Title Example",
                passportDates: "October 20 - November 3, 2019",
                venues: [
                    Venue (
                        title: "Venue Name",
                        venueArea: "Germany",
                        venueItems: [
                                venueItem (
                                title: "Potato Dumpling",
                                productDescription: "Potato Dumpling with Mushroom Sauce",
                                productPrice: 0.00,
                                productType: "Food",
                                newStatus: false,
                                mealPlan: false,
                                kidApproved: true,
                                vegetarian: false,
                                glutenFree: false,
                                featuredProduct: false,
                                containsAlcohol: false
                            ),
                            venueItem (
                                title: "Pork Schnitzel",
                                productDescription: "Pork Schnitzel with Mushroom Sauce and Spaetzle",
                                productPrice: 0.00,
                                productType: "Food",
                                newStatus: false,
                                mealPlan: false,
                                kidApproved: false,
                                vegetarian: false,
                                glutenFree: false,
                                featuredProduct: false,
                                containsAlcohol: false
                            )
])
]
            )

        ]

    }

}

// PassportDetails.swift //

import SwiftUI



struct PassportDetails: View {

    var passportTitle: String
    var venues: [Venue]

    var venueProd: [venueItem]



    var body: some View {

        NavigationView {
            List(self.venues) { ven in
                NavigationLink () {
                HStack {
                        Text(ven.title)
                    }
                }
            }
        }.navigationBarTitle(Text(passportTitle))
    }
}

我得到的错误是“表达式类型在没有更多上下文的情况下是模棱两可的”,我只是想访问Venue元素的标题和区域并将它们显示在列表中。

1 个答案:

答案 0 :(得分:0)

问题是您没有为NavigationLink

指定目标字段

目前,您可以使用以下内容进行测试:

List(self.venues) { ven in
    NavigationLink(destination: Text("Go here")) {
        HStack {
            Text(ven.title)
        }
    }
}