SwiftUI - 从嵌套数组中列出 objects
SwiftUI - List objects from nested array
我试图在嵌套数组中显示 object 值。下面是我想在列表元素中显示的数据模型和详细信息页面。
// 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))
}
}
我得到的错误是 "Type of expression is ambiguous without more context" 我只是想访问 Venue 元素的标题和区域并将它们显示在列表中。
问题是您没有为 NavigationLink
指定目标字段
暂时你可以这样测试:
List(self.venues) { ven in
NavigationLink(destination: Text("Go here")) {
HStack {
Text(ven.title)
}
}
}
我试图在嵌套数组中显示 object 值。下面是我想在列表元素中显示的数据模型和详细信息页面。
// 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))
}
}
我得到的错误是 "Type of expression is ambiguous without more context" 我只是想访问 Venue 元素的标题和区域并将它们显示在列表中。
问题是您没有为 NavigationLink
暂时你可以这样测试:
List(self.venues) { ven in
NavigationLink(destination: Text("Go here")) {
HStack {
Text(ven.title)
}
}
}