The basic gist is I'm building a meal planner app. I'm new to Swift and SwiftUI and this is my first project so I'm probably making a noob mistake lol Basically I want to pass the current index, which is a data object for a day and it's meals, to the DailyMealPlan view when I tap on the corresponding card. I don't get an error when I pass the currentDay variable to the view, but inside the second view file I'm not sure how I handle that data. I keep getting an error that "CurrentMealPlan is not in scope". I understand how scope works and I suspect I'm just missing something when it comes to passing the data into the second view.
ForEach code
ForEach(CurrentMealPlan.indices) { index in
let currentDay = CurrentMealPlan[index]
NavigationLink(
destination: DailyMealPlan(DailyMealPlan: currentDay))
{
MealPlanCard(
Day: "\(CurrentMealPlan[index].DayTitle)",
Breakfast: "\(CurrentMealPlan[index].Breakfast)",
Lunch: "\(CurrentMealPlan[index].Lunch)",
Dinner: "\(CurrentMealPlan[index].Dinner)"
)
}
}
DailyMealPlan view
struct DailyMealPlan: View {
var DailyMealPlan: Day = CurrentMealPlan[index]
var body: some View {
ZStack {
ScrollView {
VStack {
SingleMealCard(Meal: "Breakfast", CalCount: 500, MealInfo: "Meal info here")
SingleMealCard(Meal: "Lunch", CalCount: 500, MealInfo: "Meal info here")
SingleMealCard(Meal: "Dinner", CalCount: 500, MealInfo: "Meal info here")
}
}
}
}
}
CurrentMealPlan model
struct Day: Hashable {
var id: Int
var Date: String
var DayTitle: String
var Breakfast: String
var Lunch: String
var Dinner: String
init(id:Int=0,Date:String="",DayTitle:String="",Breakfast:String="",Lunch:String="",Dinner:String="") {
self.id = id
self.Date = Date
self.DayTitle = DayTitle
self.Breakfast = Breakfast
self.Lunch = Lunch
self.Dinner = Dinner
}
}
let CurrentMealPlan: [Day] = [
Day(
id: 0,
DayTitle: "Sunday",
Breakfast:"Oatmeal",
Lunch: "Sandwich",
Dinner: "Cheeseburger with Fries"
)
]
CurrentMealPlanmodel? It'll be easier to show what to do with access to that. By the way, it's common practice in Swift to capitalize type names, but leave variable/property names in lowercase.= CurrentMealPlan[index].