UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Sorting of Dates within an If and Else? and Storing them in Core Data.

Forums > Swift

@Jawds  

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

2      

Hi, how does the .xcdatamodel file look like? Does it have all the properties as Food class?

2      

Have you installed the app, changed the data model in Xcode and reinstalled it? It seems so ("Persistent store migration failed, missing mapping model."). You have to delete the app from the simulator and install it again.

If you change the data model in a way that Core Data can't handle the changes in your model automatically you have to provide either a mapping model or a custom migration policy.

2      

@Jawds  

Thanks so much. All I had to do was delete the app in the simulator and reinstall.

Very much appreciated.

2      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.