0

A newbie in Swift here,

I have a Meal struct and I also have an array of Meal struct objects. Since Firestore doesn't have group-by option built in, I attempted to group the Meal array that I fetched from Firestore using Dictionary(grouping: ) initializer. I grouped the Meals by date of consumption, so I could then count the calories for a specific day.

Here's a snippet of my dictionary so far, I skipped some unnecessary fields:

["28 November 2021": [HealthyChoices.Meal(id: "...", calories_eaten: 40, meal_products: ["..."], date: "28 November 2021", who_ate: "..."), HealthyChoices.Meal(id: "...", calories_eaten: 50, meal_products: ["..."], date: "28 November 2021", who_ate: "...")]

As you can see, the key is the date by which the meals were grouped, and the value is the whole object fetched from Firestore. There can be of course many meals assigned to one date. Now what I want to do is to calculate the total calories eaten for a given day (key in this dictionary). Unfortunately I have no bloody idea what would be the way to iterate through this to get only this one specific field from every Meal (the "calories_eaten" field) and then count it for every date (the key in this dictionary).

I was trying to accomplish something with the map function but unfortunately seems I still cannot access the fields. I was trying something like this:

for (key, value) in groupedMeals {
     for key in groupedMeals.keys {
            value.get //no idea how to access that
     }
}

Please help, I give cookies.

4
  • Your value variable already contains the array of meals that correspond to the particular key. You just use it like any ol’ array. Commented Nov 29, 2021 at 20:29
  • Hey Alexander. I don't think I understand it correctly. If I try to access it like in the example loop that I showed above, e.g. value.calories_eaten, I get a "Value of type '[Meal]' has no member 'calories_eaten'" error. I'm also not sure if the nested for loops that I provided above are sufficient for accessing what I meant to or not Commented Nov 29, 2021 at 20:57
  • Yes, because it’s a [Meal] (a.k.a. An Array<Meal>). It’s multiple meals, there isn’t one single “calories_eaten” property. You need to access the members of that array, such as with a nested loop. But the nest loop needing to be looping over this array of meals, not groupMeal.keys (Which will be the dictionary’s keys, I.e. the time stamps) Commented Nov 29, 2021 at 21:02
  • Thank you! Thanks to your advice I was able to access the values finally. Commented Nov 30, 2021 at 10:02

1 Answer 1

1

You can use .mapValues on the dictionary:

var caloriesPerDay: Dictionary<String, Int> = groupedMeals.mapValues { meals in
    meals.map { meal in
        meal.calories_eaten // Get calories_eaten from each meal
    }.reduce(0, +) // Sum of the resulting [Int]
}
Sign up to request clarification or add additional context in comments.

1 Comment

Great, thanks, this works like a charm! I guess I need to take some dictionaries repetition lessons :)

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.