O'Reilly logo

Learn iOS 11 Programming with Swift 4 - Second Edition by Craig Clayton

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Updating our Core Data manager 

Now that we have our favorite entity created, we need to update our Core Data manager to actually save restaurants as favorites. Inside of the CoreData-Manager.swift file, add the following before the last curly brace:

func addFavorite(by restaurantID:Int) {   let item = Favorite(context: container.viewContext)   item.restaurantID = Int32(restaurantID)
   save()}

This method creates a Favorite object and then calls the save() method.

Now, let's add one more method to this file by adding the following before the last curly brace:

func isFavorite(with identifier:Int) -> Bool {   let moc = container.viewContext   let request:NSFetchRequest<Favorite> = Favorite.fetchRequest() let predicate = NSPredicate(format: "restaurantID ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required