我正在尝试在嵌套数组中显示对象值。下面是我想在列表元素中显示的数据模型和详细信息页面。
护照.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
)
])
]
)
]
}
}
护照详细信息.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 元素的标题和区域并在列表中显示它们。
问题是您没有为NavigationLink
指定目标字段
目前,您可以使用以下内容进行测试:
List(self.venues) { ven in
NavigationLink(destination: Text("Go here")) {
HStack {
Text(ven.title)
}
}
}