Hi there,
New Swift beginner/amateur here. I am currently working on a Food Diary app, that allows the user to enter the items they have eaten throughout the day.
I am currently having trouble with sorting different entries that the user has entered.
Firstly, I am attempting to use an if and else to sort the different entries that the user has put in. I based the CoreData code mainly off of the "BookWorm" app that is the 11th Project of 100 Days of SwiftUI.
This is the if and else used to sort the different entries:
Section {
Text("Breakfast")
.font(.title)
ForEach(foods, id: \.self) { food in
let calendar = Calendar.current
let day = calendar.component(.day, from: entrydate) // entrydate is the date that the user enters to dictate which day they would like to see
// this code turns the date entered by the user into a day which can be compared to other days
if (food.mealTime == "Breakfast") && (food.date == day) { // food.date is the date within CoreData related to the food
Text(food.food ?? "Unknown Food")
Text(" Calories: \(food.calories)")
.font(.caption)
}
}.onDelete(perform: deleteFoods)
}
To sort the dates of each food, I had hoped that I could easily take the day from each Date() that the user had entered and compared so that if the dates were the same, the food entered would show.
When attempting this however, I recieve a fatal error. (NSCocoaErrorDomain Code=134140 "Persistent store migration failed, missing mapping model.")
For reference, this is the code that I use to enter the different attributes into the data model.
Section {
Button("Add to Diary") {
let newEntry = Food(context: self.moc)
let calendar = Calendar.current
let day = calendar.component(.day, from: date)
newEntry.food = self.food
newEntry.calories = Int16(calories)!
newEntry.mealTime = self.mealTime
newEntry.date = Int16(day)
try? self.moc.save()
self.presentationMode.wrappedValue.dismiss()
}
}
Let me know if any more information/clarification is needed to solve this problem. Any help is appreciated as this is the basis of my final assignment for my Year 12 Digital Technologies class.
Thanks,
Jawds