I'm creating a table view that will track meals. For context, I have two other swift files each with a struct Meal and the other is a Food struct (as shown in code below in the "meals" variable initialized.
var meals: [Meal] = [Meal(name: "breakfast", food: [Food(name: "eggs", description: "3 eggs")]), Meal(name: "lunch", food: [Food(name: "sandwich", description: "ham and cheese sandwich")]), Meal(name: "dinner", food: [Food(name: "steak", description: "steak with a side of potatoes")])]
What I'm being asked is how to return the number of rows in each section (each section being a meal object).
I got the numberofSections to return the number of sections by typing meals.count to return the value of objects:
override func numberOfSections(in tableView: UITableView) -> Int {
return meals.count
}
Now I need to access the amount of "Food" objects in each the meal objects in the numberOfRows:
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return meals.food.count
}
But I get an error that says "Value of type [Meal] has no member "food". I don't know what else to do. In this case I only put one food object in each meal object, so I know the value will be 1, but I need to access the number from the variable directly.
Any help is appreciated!