1

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!

1 Answer 1

1

You need to get the count of the food array for the individual Meal that corresponds to the section in that method. So the simple solution is like so:

return meals[section].food.count
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.